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

熱點內容
破鎖屏密碼方法 瀏覽:835
股票數據放哪裡 瀏覽: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

友情鏈接