.net經過網絡路徑上傳文件至服務器

獲取網絡文件,經過流保存文件。網絡

//網絡路徑文件ip

string pathUrl = "http://localhost:805/js/site.zip";
System.Net.HttpWebRequest request = null;
System.Net.HttpWebResponse response = null;
//請求網絡路徑地址
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(pathUrl);
request.Timeout = 5000; // 超時時間
//得到請求結果
response = (System.Net.HttpWebResponse)request.GetResponse();
//文件下載地址
string path = "/文件下載";
// 若是不存在就建立file文件夾
if (!Directory.Exists(path))
{
if (path != null) Directory.CreateDirectory(path);
}
path = path + "/site.zip";
//長度
byte[] img = new byte[response.ContentLength];
Stream i = response.GetResponseStream();
//讀取二進制流
i.Read(img, 0, Convert.ToInt32(response.ContentLength));//這裏獲取以後
i.Close();
i.Dispose();
//保存爲指定文件
File.WriteAllBytes(path, img);
response.Close();string

相關文章
相關標籤/搜索