標籤:c#
/// <summary>
/// 網頁下載檔案
/// </summary>
/// <param name="DownloadPath">目標地址</param>
/// <param name="FullFilePath">原地址</param>
/// <param name="FileName">檔案名稱</param>
/// <returns></returns>
public static bool DownLoadSoft(string DownloadPath, string FullFilePath, string FileName)
{
bool flag = false;
try
{
if (!Directory.Exists(DownloadPath))
{
Directory.CreateDirectory(DownloadPath);
}
using (FileStream fs = new FileStream(DownloadPath + "/" + FileName, FileMode.Create))
{
//建立請求
WebRequest request = WebRequest.Create(FullFilePath + FileName);
//接收響應
WebResponse response = request.GetResponse();
//輸出資料流
Stream responseStream = response.GetResponseStream();
byte[] bufferBytes = new byte[10000];//緩衝位元組數組
int bytesRead = -1;
while ((bytesRead = responseStream.Read(bufferBytes, 0, bufferBytes.Length)) > 0)
{
fs.Write(bufferBytes, 0, bytesRead);
}
if (fs.Length > 0)
{
flag = true;
}
//關閉寫入
fs.Flush();
fs.Close();
}
}
catch (Exception exp)
{
//返回錯誤訊息
}
return flag;
}
c# ASP.NET 下載檔案到本地