private bool DownLoadFile()
{
string url =System.Configuration.ConfigurationManager.AppSettings["WebDownLoadUrl"].ToString();
string param = "?filename="+FileName;
try
{
string strDirectory =PublicClass.SysGlobal.BasePath + "\\" + AcceptCaseNo+"\\";
string strPath = strDirectory + FileName;
if (SavePath == "upload")
{
strPath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["UpLoadFiles"].ToString() + "\\" + FileName);
}
else
{
if (!System.IO.Directory.Exists(strDirectory))
System.IO.Directory.CreateDirectory(strDirectory);
}
WebRequest request = WebRequest.Create(url+param);
request.Timeout = 10000;
using (WebResponse response = request.GetResponse())
{
Stream reader = response.GetResponseStream();
FileStream writer = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write);
byte[] buff = new byte[10240];
int c = 0; //實際讀取的字節數
while ((c = reader.Read(buff, 0, buff.Length)) > 0)
{
writer.Write(buff, 0, c);
}
writer.Close();
writer.Dispose();
reader.Close();
reader.Dispose();
response.Close();
}
}
catch (WebException ex)
{
string log = UserID + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "]" +
"從外網下載文件失敗!"+ "無法連接到服務器\r\n錯誤信息:" + ex.Message;
new BasePage().WriteWebLog(CaseID,log);//寫日誌
return false;
}
return true;
}
C# 網上下載文件或圖片
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
cocos2d-x 3.10 安卓 屏幕適配
dutong0321
2020-06-30 23:16:00
js 裏 escape,encodeURI,encodeURIComponent編碼方法之我見
仁者乐于水
2020-06-09 14:35:31
Windows 2000/XP/2003中刪除隱藏網卡 設置IP方法
仁者乐于水
2020-06-09 14:35:20
Oracle10g ORA-12170:TNS:連接超時
仁者乐于水
2020-06-09 14:35:20
關於forEach遍歷DOM
dutong0321
2020-04-20 22:58:17
javascript更新元素加載延遲及圖片src
dutong0321
2020-02-25 17:05:43
C++多線程編程
dutong0321
2020-02-25 17:05:43
如何使用logminer查看日誌內容
仁者乐于水
2020-02-24 13:29:53
javascript 單選或複選剩餘一項時數據取值問題如何解決
仁者乐于水
2020-02-24 13:29:43
IIS發生意外錯誤0x8ffe2740 大多數是80端口被其他程序佔用了(Apache、迅雷)
仁者乐于水
2020-02-24 13:29:43
IE6 中 window.location.href 不能跳轉
仁者乐于水
2020-02-24 13:29:43
.net Mail 發郵件類
仁者乐于水
2020-02-24 13:29:43
C#2.0中,SerialPort如何讀取串口數據並顯示在TextBox上
仁者乐于水
2020-02-24 13:29:43
Javascript (js)調用後臺代碼方法彙總
仁者乐于水
2020-02-24 13:29:43
24小時熱門文章