導航:首頁 > 編程語言 > 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下載鏈接代碼相關的資料

熱點內容
proface密碼 瀏覽:190
什麼app幫別人買感冒葯 瀏覽:365
華為終端客服微信 瀏覽:33
文件後面加內容應該怎麼取名 瀏覽:959
可以學會講白話的app有哪些 瀏覽:332
mysql資料庫navicat 瀏覽:246
word如何固定文本框 瀏覽:974
什麼文件轉換為pst 瀏覽:76
編程在哪裡好找工作 瀏覽:177
電腦圖標上有個文件 瀏覽:832
筆記本i5系統重裝教程 瀏覽:472
文件如何變白 瀏覽:679
長城c30加裝導航升級 瀏覽:648
壓縮包會改變文件內容么 瀏覽:548
45度面槽怎麼編程 瀏覽:742
蘋果6原裝充電器多大的 瀏覽:814
騰訊群文件 瀏覽:584
win10雙擊excel文件無法直接打開 瀏覽:152
dnf90版本劍魂右槽排名 瀏覽:375
mac給文件夾添加快捷方式到桌面 瀏覽:221

友情鏈接