导航:首页 > 编程语言 > 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下载链接代码相关的资料

热点内容
打开文件夹出错 浏览:825
如何清除苹果app的文件和数据 浏览:515
如何打开crv文件 浏览:41
md文件夹在win7不能打开 浏览:668
颂拓手表运动数据如何导入微信 浏览:654
什么网站信访最有效 浏览:396
魅蓝e2升级安卓70 浏览:438
黄石响应式网站建设多少钱 浏览:410
怎么把excel工作簿放到一个文件夹 浏览:949
wifi网络延时大怎么处理 浏览:345
云办公的原理是把传统文件放哪里 浏览:113
不属于群防群治队伍数据项有哪些 浏览:404
java树向上找 浏览:241
数据库查询票价 浏览:503
word黑色下划线怎么去掉 浏览:879
学习编程怎么学比较好 浏览:351
有什么好看的地图网站 浏览:593
oppo如何设置app黑名单 浏览:71
移动数据用了多少在哪里显示 浏览:549
excel表改变文件名颜色的方法 浏览:966

友情链接