㈠ 求一段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>
<head>代碼下載</head>
<title>代碼下載</title>
<body>
<ahref="Web.rar">download</a>
</body>
</html
瀏覽器支持能夠打開的格式,他都會默認直接在線打開(比如word或圖片),不支持的格式,他就會彈出下載提示。最好是做成.rar格式的文件。