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

热点内容
股票数据放哪里 浏览:576
m格式库文件 浏览:279
天际通数据服务怎么开票 浏览:430
写小说发哪个网站比较好 浏览:244
小米电视3蓝牙文件路径 浏览:111
shell读取文件值 浏览:909
文件夹路径栏消失 浏览:795
律师哪些业务不能代替大数据 浏览:952
lol哪些文件可以删除 浏览:701
汇编程序中del是什么意思 浏览:183
幼儿园免费网站模板下载 浏览:210
w619线刷教程 浏览:759
怎么培养编程思想 浏览:697
手机捆绑app的软件怎么卸载 浏览:32
vb编程器有什么用 浏览:999
excel如何分列数据与文字 浏览:884
512网络病毒 浏览:994
java调用google地图api接口 浏览:726
文件管理图片小窗 浏览:599
显卡数据怎么设置 浏览:557

友情链接