导航:首页 > 编程语言 > html下载链接代码

html下载链接代码

发布时间:2024-05-23 06:53:28

㈠ 求一段html代码!下载文件到本地 就是指定一个链接。然后下载那个文件保存到电脑上

#region 下载文件

/**//// <summary>
/// 从FTP服务器下载文件,使用与远程文件同名的文件名来保存文件
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
/// <param name="LocalPath">本地路径</param>

public bool DownloadFile(string RemoteFileName, string LocalPath)
{
return DownloadFile(RemoteFileName, LocalPath, RemoteFileName);
}
/**//// <summary>
/// 从FTP服务器下载文件,指定本地路径和本地文件名
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
/// <param name="LocalPath">本地路径</param>
/// <param name="LocalFilePath">保存文件的本地路径,后面带有"\"</param>
/// <param name="LocalFileName">保存本地的文件名</param>
public bool DownloadFile(string RemoteFileName, string LocalPath, string LocalFileName)
{
byte[] bt = null;
try
{
if (!IsValidFileChars(RemoteFileName) || !IsValidFileChars(LocalFileName) || !IsValidPathChars(LocalPath))
{
throw new Exception("非法文件名或目录名!");
}
if (!Directory.Exists(LocalPath))
{
throw new Exception("本地文件路径不存在!");
}

string LocalFullPath = Path.Combine(LocalPath, LocalFileName);
if (File.Exists(LocalFullPath))
{
throw new Exception("当前路径下已经存在同名文件!");
}
bt = DownloadFile(RemoteFileName);
if (bt != null)
{
FileStream stream = new FileStream(LocalFullPath, FileMode.Create);
stream.Write(bt, 0, bt.Length);
stream.Flush();
stream.Close();
return true;
}
else
{
return false;
}
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}

/**//// <summary>
/// 从FTP服务器下载文件,返回文件二进制数据
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
public byte[] DownloadFile(string RemoteFileName)
{
try
{
if (!IsValidFileChars(RemoteFileName))
{
throw new Exception("非法文件名或目录名!");
}
Response = Open(new Uri(this.Uri.ToString() + RemoteFileName), WebRequestMethods.Ftp.DownloadFile);
Stream Reader = Response.GetResponseStream();

MemoryStream mem = new MemoryStream(1024 * 500);
byte[] buffer = new byte[1024];
int bytesRead = 0;
int TotalByteRead = 0;
while (true)
{
bytesRead = Reader.Read(buffer, 0, buffer.Length);
TotalByteRead += bytesRead;
if (bytesRead == 0)
break;
mem.Write(buffer, 0, bytesRead);
}
if (mem.Length > 0)
{
return mem.ToArray();
}
else
{
return null;
}
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion

㈡ python爬虫 将在线html网页中的图片链接替换成本地链接并将html文件下载到本地

import os,re
def check_flag(flag):
regex = re.compile(r'images\/')
result = True if regex.match(flag) else False
return result

#soup = BeautifulSoup(open('index.html'))
from bs4 import BeautifulSoup
html_content = '''
<a href="https://xxx.com">测试01</a>
<a href="https://yyy.com/123">测试02</a>
<a href="https://xxx.com">测试01</a>
<a href="https://xxx.com">测试01</a>
'''
file = open(r'favour-en.html','r',encoding="UTF-8")
soup = BeautifulSoup(file, 'html.parser')
for element in soup.find_all('img'):
if 'src' in element.attrs:
print(element.attrs['src'])
if check_flag(element.attrs['src']):
#if element.attrs['src'].find("png"):
element.attrs['src'] = "michenxxxxxxxxxxxx" +'/'+ element.attrs['src']

print("##################################")
with open('index.html', 'w',encoding="UTF-8") as fp:
fp.write(soup.prettify()) # prettify()的作⽤是将sp美化⼀下,有可读性

㈢ html下载文件代码,就是点击按钮下载指定文件

1、在a标签中指定href='文件路径',download='文件名';这样直接点a标签就能下载文件了。
2、给按钮绑回定个click事件,在答事件里使用window.location.href='文件路径',或者window. open("文件路径")
3、如果需要从后台查询文件,也可以直接后台返回流也行的

㈣ html怎么实现网页中文件下载功能

共两种方法:

一、使用<a>标签来完成

㈤ html中点击下载的代码怎么写

阅读全文

与html下载链接代码相关的资料

热点内容
联想启天m7300能升级内存吗 浏览:698
精雕怎么打开图片文件 浏览:161
ug编程镗孔g76如何使用 浏览:719
7岁到18岁少儿编程学什么 浏览:413
mac文件怎么打印 浏览:756
农药登记许可证查询哪个网站 浏览:857
素材站用什么网站系统 浏览:173
ug如何用铰刀编程 浏览:647
三国志版本介绍 浏览:260
情侣用的定位app有哪些 浏览:244
怎么清楚苹果手机的健身数据 浏览:988
淘宝美工教程自学网 浏览:452
父子2006未删减版本 浏览:872
包头谁卖苹果6 浏览:504
化工企业停工有哪些文件通知要求 浏览:878
下载软件后文件夹在桌面 浏览:397
word文字编组 浏览:167
旅游策划的程序 浏览:519
哪些app能看象棋直播 浏览:819
小米4怎么传文件 浏览:210

友情链接