/// <summary> /// 远程提取文件保存至服务器上(使用WebRequest) /// </summary> /// <param name="url">一个URI上的文件</param> /// <param name="saveurl">文件保存在服务器上的全路径</param> public static void RemoteGetFile(string url, string saveurl) { HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse(); Stream stream = myWebResponse.GetResponseStream(); //获得请求的文件大小 int fileSize = (int)myWebResponse.ContentLength; int bufferSize = 102400; byte[] buffer = new byte[bufferSize]; FObject.WriteFile(saveurl, "temp"); // 建立一个写入文件的流对象 FileStream saveFile = File.Create(saveurl, bufferSize); int bytesRead; do { bytesRead = stream.Read(buffer, 0, buffer.Length); saveFile.Write(buffer, 0, bytesRead); } while (bytesRead > 0); saveFile.Flush(); saveFile.Close(); stream.Flush(); stream.Close(); }
/// <summary> /// 远程提取一个文件保存至服务器上(使用WebClient) /// </summary> /// <param name="url">一个URI上的文件</param> /// <param name="saveurl">保存文件</param> public static void WebClientGetFile(string url, string saveurl) { WebClient wc = new WebClient(); try { FObject.WriteFile(saveurl, "temp"); wc.DownloadFile(url, saveurl); } catch { } wc.Dispose(); }