/// <summary> /// 下载文件方法 /// </summary> /// <param name="localFilePath"></param> /// <param name="url"></param> /// <returns></returns> public static bool Download(string localFilePath, string url) { bool flag = false; //打开上次下载的文件 long SPosition = 0; //实例化流对象 FileStream FStream; //判断要下载的文件夹是否存在 string ParentDirectory = Path.GetDirectoryName(localFilePath); if (!Directory.Exists(ParentDirectory)) { Directory.CreateDirectory(ParentDirectory); } if (File.Exists(localFilePath)) { //打开要下载的文件 FStream = File.OpenWrite(localFilePath); //获取已经下载的长度 SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); } else { //文件不保存创建一个文件 FStream = new FileStream(localFilePath, FileMode.Create); SPosition = 0; } try { //打开网络连接 HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url); long length = myRequest.GetResponse().ContentLength; if (SPosition > 0) { myRequest.AddRange((int)SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 Stream myStream = myRequest.GetResponse().GetResponseStream(); //定义一个字节数据 byte[] btContent = new byte[512]; int intSize = 0; intSize = myStream.Read(btContent, 0, 512); while (intSize > 0) { FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, 512); } //关闭流 FStream.Close(); myStream.Close(); flag = true; //返回true下载成功 } catch (Exception e) { FStream.Close(); flag = false; //返回false下载失败 } return(flag); }
//移入ZIP类 /// <summary> /// 从指定站点下载文件,并保存在指定位置,开始位置,完成度(用于ProgressBar使用) /// </summary> /// <param name="strFileName">本地物理路径</param> /// <param name="strUrl">目标Url</param> public static void DownloadFile(string strFileName, string strUrl, int begin, string flag) { if (SafeSC.FileNameCheck(strFileName)) { throw new Exception(strFileName + "取消保存"); } //已完成的,1%长度 int CompletedLength = 0; long percent = 0; progStatus = "0"; string temp = "0"; long sPosstion = 0; //磁盘现盘文件的长度 //long count = 0;// count += sPosstion,从指定位置开始写入字节 FileStream FStream; if (File.Exists(strFileName)) //如果文件存在 { FStream = File.OpenWrite(strFileName); //打开继续写入,并从尾部开始,用于断点续传 sPosstion = FStream.Length; FStream.Seek(sPosstion, SeekOrigin.Current); //移动文件流中的当前指针 } else { FStream = new FileStream(strFileName, FileMode.Create); sPosstion = 0; } //打开网络连接 try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl); //if (CompletedLength > 0) // myRequest.AddRange((int)CompletedLength);//设置Range值,即头,从指定位置开始接收文件.. //向服务器请求,获得服务器的回应数据流 HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse(); long FileLength = webResponse.ContentLength;//文件大小 percent = FileLength / 100; Stream myStream = webResponse.GetResponseStream(); byte[] btContent = new byte[1024]; //if (count <= 0) count += sPosstion;// //开始写入 int count = 0; while ((count = myStream.Read(btContent, 0, 1024)) > 0) //返回读了多少字节,为0表示全部读完 { FStream.Write(btContent, 0, count); //知道有多少个数字节后再写入 CompletedLength += count; if (!(CompletedLength / percent).ToString().Equals(temp)) { temp = (CompletedLength / percent).ToString(); progStatus = temp; } } myStream.Close(); } finally { FStream.Close(); } }
/// /// 下载文件方法 /// /// 文件保存路径和文件名 /// 返回服务器文件名 /// bool DownloadFile(string DownUrl, string SaveFileName) { bool flag = false; //打开上次下载的文件 long SPosition = 0; //实例化流对象 FileStream FStream; //判断要下载的文件夹是否存在 if (File.Exists(SaveFileName)) { //打开要下载的文件 FStream = File.OpenWrite(SaveFileName); //获取已经下载的长度 SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); } else { //文件不保存创建一个文件 FStream = new FileStream(SaveFileName, FileMode.Create); SPosition = 0; } try { //打开网络连接 HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(DownUrl); if (SPosition > 0) { myRequest.AddRange((int)SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 Stream myStream = myRequest.GetResponse().GetResponseStream(); //定义一个字节数据 byte[] btContent = new byte[512]; int intSize = 0; intSize = myStream.Read(btContent, 0, 512); while (intSize > 0) { FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, 512); } //关闭流 FStream.Close(); myStream.Close(); flag = true; //返回true下载成功 } catch (Exception) { FStream.Close(); flag = false; //返回false下载失败 } return(flag); }
/// <summary> /// 以断点续传方式下载文件 /// </summary> /// <param name="strFileName">下载文件的保存路径</param> /// <param name="strUrl">文件下载地址</param> public static void DownloadFile(string strUrl) { var strFileName = AppDomain.CurrentDomain.BaseDirectory + @"DownloadGeam\" + strUrl.Substring(strUrl.LastIndexOf("/") + 1); float percent = 0; //打开上次下载的文件或新建文件 long SPosition = 0; FileStream FStream; if (File.Exists(strFileName)) { FStream = File.OpenWrite(strFileName); SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current);//移动文件流中的当前指针 } else { FStream = new FileStream(strFileName, FileMode.Create); SPosition = 0; } //打开网络连接 try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl); if (SPosition > 0) { myRequest.AddRange((int)SPosition);//设置Range值 } long totalBytes = myRequest.ContentLength; //向服务器请求,获得服务器的回应数据流 Stream myStream = myRequest.GetResponse().GetResponseStream(); byte[] btContent = new byte[1024]; int intSize = 0; long totalDownloadedByte = 0; intSize = myStream.Read(btContent, 0, 1024); while (intSize > 0) { totalDownloadedByte = intSize + totalDownloadedByte; FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, 1024); percent = totalDownloadedByte / (float)totalBytes * 100; } FStream.Close(); myStream.Close(); } catch (Exception ex) { FStream.Close(); } }
/// <summary> /// 以断点续传方式下载文件 /// </summary> /// <param name="strFileName">下载文件的保存路径</param> /// <param name="strUrl">文件下载地址</param> public void DownloadFile(string strFileName, string strUrl) { //打开上次下载的文件或新建文件 int CompletedLength = 0;//记录已完成的大小 FileStream FStream; if (File.Exists(strFileName)) { FStream = File.OpenWrite(strFileName); sPosstion = FStream.Length; FStream.Seek(sPosstion, SeekOrigin.Current);//移动文件流中的当前指针 } else { FStream = new FileStream(strFileName, FileMode.Create); sPosstion = 0; } //打开网络连接 try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl); if (CompletedLength > 0) { myRequest.AddRange((int)CompletedLength);//设置Range值 } //向服务器请求,获得服务器的回应数据流 HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse(); FileLength = webResponse.ContentLength;//文件大小 Stream myStream = webResponse.GetResponseStream(); byte[] btContent = new byte[1024]; if (count <= 0) { count += sPosstion; } while ((CompletedLength = myStream.Read(btContent, 0, 1024)) > 0) { FStream.Write(btContent, 0, CompletedLength); count += CompletedLength; } FStream.Close(); myStream.Close(); MessageBox.Show("文件下载完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { FStream.Close(); } }
/// <summary> /// 从指定站点下载文件,并保存在指定位置,//下载,迁入ZipClass /// </summary> /// <param name="strUrl">目标Url</param> /// <param name="strFileName">本地物理路径</param> /// <param name="flag">appliatoin[flag]用于存进度</param> /// <param name="ct">当前上下文HttpContext</param> /// <param name="begin">从指定位置开始下载(未实现)</param> public void DownloadFile(string strUrl, string strFileName, string flag = "", HttpContext ct = null, int begin = 0) { //已完成的,1%长度 long percent = 0; long sPosstion = 0; //磁盘现盘文件的长度 //long count = 0;// count += sPosstion,从指定位置开始写入字节 FileStream FStream; if (File.Exists(strFileName)) //如果文件存在 { FStream = File.OpenWrite(strFileName); //打开继续写入,并从尾部开始,用于断点续传 sPosstion = FStream.Length; FStream.Seek(sPosstion, SeekOrigin.Current); //移动文件流中的当前指针 } else { FStream = new FileStream(strFileName, FileMode.Create); sPosstion = 0; } //打开网络连接 try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl); //if (CompletedLength > 0)//断点续传 // myRequest.AddRange((int)CompletedLength);//设置Range值,即头,从指定位置开始接收文件.. //向服务器请求,获得服务器的回应数据流 HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse(); long FileLength = webResponse.ContentLength;//文件大小 percent = FileLength / 100; Stream myStream = webResponse.GetResponseStream(); byte[] btContent = new byte[myStream.Length]; myStream.Read(btContent, 0, btContent.Length); SafeSC.SaveFile(Path.GetDirectoryName(strFileName) + "\\", Path.GetFileName(strFileName), btContent); //while ((count = myStream.Read(btContent, 0, 1024)) > 0)//返回读了多少字节,为0表示全部读完 //{ // FStream.Write(btContent, 0, count);//知道有多少个数字节后再写入 // CompletedLength += count; // if(ct!=null&&!string.IsNullOrEmpty(flag)) // ct.Application[flag] = CompletedLength; //} myStream.Close(); } finally { FStream.Close(); } }
/// <summary> /// 以断点续传方式下载文件 /// </summary> /// <param name="strFileName">下载文件的保存路径</param> /// <param name="strUrl">文件下载地址</param> public void DownloadFile(string strFileName, string strUrl) { //打开上次下载的文件或新建文件 long SPosition = 0; FileStream FStream; if (File.Exists(strFileName)) { FStream = File.OpenWrite(strFileName); SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current);//移动文件流中的当前指针 } else { FStream = new FileStream(strFileName, FileMode.Create); SPosition = 0; } //打开网络连接 try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl); if (SPosition > 0) { myRequest.AddRange((int)SPosition);//设置Range值 } //向服务器请求,获得服务器的回应数据流 Stream myStream = myRequest.GetResponse().GetResponseStream(); byte[] btContent = new byte[512]; int intSize = 0; intSize = myStream.Read(btContent, 0, 512); while (intSize > 0) { FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, 512); } FStream.Close(); myStream.Close(); MessageBox.Show("文件下载完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { FStream.Close(); } }
/// <summary> /// 从指定服务器上下载文件,支持断点续传 /// </summary> /// <param name="url">目标Url</param> /// <param name="vpath">本地虚拟路径</param> /// <param name="begin">开始位置,默认为0</param> public void DownFile(string url, string vpath, int begin = 0) { vpath = SafeSC.PathDeal(vpath); if (SafeSC.FileNameCheck(vpath)) { throw new Exception("不支持下载[" + Path.GetFileName(vpath) + "]文件"); } string ppath = function.VToP(vpath); //已完成的,1%长度 int CompletedLength = 0; long percent = 0; string temp = "0"; long sPosstion = 0;//磁盘现盘文件的长度 //long count = 0;// count += sPosstion,从指定位置开始写入字节 FileStream FStream; if (File.Exists(ppath)) { FStream = File.OpenWrite(ppath); //打开继续写入,并从尾部开始,用于断点续传(如果不需要,则应该删除其) sPosstion = FStream.Length; FStream.Seek(sPosstion, SeekOrigin.Current); //移动文件流中的当前指针 } else { string dir = Path.GetDirectoryName(ppath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } FStream = new FileStream(ppath, FileMode.Create); sPosstion = 0; } //打开网络连接 try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url); //if (CompletedLength > 0) // myRequest.AddRange((int)CompletedLength);//设置Range值,即头,从指定位置开始接收文件.. //向服务器请求,获得服务器的回应数据流 HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse(); long FileLength = webResponse.ContentLength;//文件大小 percent = FileLength / 100; Stream myStream = webResponse.GetResponseStream(); byte[] btContent = new byte[1024]; //if (count <= 0) count += sPosstion;// //开始写入 int count = 0; while ((count = myStream.Read(btContent, 0, 1024)) > 0) //返回读了多少字节,为0表示全部读完 { FStream.Write(btContent, 0, count); //知道有多少个数字节后再写入 CompletedLength += count; if (!(CompletedLength / percent).ToString().Equals(temp)) { temp = (CompletedLength / percent).ToString(); //progStatus = temp; } } myStream.Close(); } finally { FStream.Close(); } }
/// /// 下载文件方法 支持断点续传 /// /// 文件保存路径和文件名 /// 服务器文件名 public bool DownloadFile(CookieContainer cookie, string strFileName, string fileUrl) { bool flag = false; //打开上次下载的文件 long SPosition = 0; //实例化流对象 FileStream FStream; //判断要下载的文件是否存在 if (File.Exists(strFileName)) { //打开要下载的文件 FStream = File.OpenWrite(strFileName); //获取已经下载的长度 SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); //File.Delete(strFileName); } else { //文件不保存创建一个文件 FStream = new FileStream(strFileName, FileMode.Create); SPosition = 0; } HttpWebRequest myRequest = null; HttpWebResponse myResponse = null; try { //打开网络连接 myRequest = (HttpWebRequest)HttpWebRequest.Create(GetUrl(fileUrl)); //myRequest.AllowAutoRedirect = true; myRequest.CookieContainer = cookie; //OnDownloadProgress(SPosition, total ,strFileName ); if (SPosition > 0) { myRequest.AddRange((int)SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 myResponse = myRequest.GetResponse() as HttpWebResponse; //myResponse.Headers.Add("content-reanghe", SPosition.ToString() ); long total = myResponse.ContentLength; Stream myStream = myResponse.GetResponseStream(); //定义一个字节数据 int bufflength = 1024; byte[] btContent = new byte[bufflength]; int intSize = 0; intSize = myStream.Read(btContent, 0, bufflength); SPosition += intSize; OnDownloadProgress(SPosition, total, strFileName); while (intSize > 0) { FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, bufflength); SPosition += intSize; OnDownloadProgress(SPosition, total, strFileName); } myStream.Close(); flag = true; //返回true下载成功 OnDownloadCompleted(null, true, null); } catch (Exception ex) { flag = false; //返回false下载失败 OnDownloadCompleted(ex, false, strFileName); } finally { //关闭流 if (FStream != null) { FStream.Close(); FStream = null; } if (myResponse != null) { myResponse.Close(); myResponse = null; } if (myRequest != null) { myRequest.Abort(); myRequest = null; } } return(flag); }
//Html必须编码后再传输,html默认的是将其UrlEncode后传递,服务端再编辑,直接传是不能的 //public M_Http_Result UploadParam(string url, Dictionary<string, object> postParameters) //{ // HttpMultipartFormRequest req = new HttpMultipartFormRequest(); // //req.config = config; // M_Http_Result result = new M_Http_Result(); // result = req.AsyncHttpRequest(url, postParameters, null); // return result; //} #endregion #region 文件下载 /// <summary> /// 从指定服务器上下载文件,支持断点续传 /// </summary> /// <param name="url">目标Url</param> /// <param name="vpath">本地虚拟路径</param> /// <param name="begin">开始位置,默认为0</param> public static void DownFile(string url, string vpath, int begin = 0) { //尝试除以0,原因:下载的文字过小,占用字节为0 vpath = SafeSC.PathDeal(vpath); if (SafeSC.FileNameCheck(vpath)) { throw new Exception("不支持下载[" + Path.GetFileName(vpath) + "]文件"); } string ppath = function.VToP(vpath); //long percent = 0; long sPosstion = 0;//磁盘现盘文件的长度 //long count = 0;// count += sPosstion,从指定位置开始写入字节 FileStream FStream; if (File.Exists(ppath)) { FStream = File.OpenWrite(ppath); //打开继续写入,并从尾部开始,用于断点续传(如果不需要,则应该删除其) sPosstion = FStream.Length; FStream.Seek(sPosstion, SeekOrigin.Current); //移动文件流中的当前指针 } else { string dir = Path.GetDirectoryName(ppath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } try { FStream = new FileStream(ppath, FileMode.Create); } catch (Exception ex) { throw new Exception(ex.Message + "||" + ppath); } sPosstion = 0; } //打开网络连接 Stream myStream = null; try { HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url); //if (CompletedLength > 0) // myRequest.AddRange((int)CompletedLength);//设置Range值,即头,从指定位置开始接收文件.. //向服务器请求,获得服务器的回应数据流 HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse(); //long FileLength = webResponse.ContentLength;//文件大小 //percent = FileLength / 100; myStream = webResponse.GetResponseStream(); byte[] btContent = new byte[1024]; //开始写入 int count = 0; while ((count = myStream.Read(btContent, 0, 1024)) > 0) //返回读了多少字节,为0表示全部读完 { FStream.Write(btContent, 0, count); //知道有多少个数字节后再写入 } } catch (Exception ex) { if (!ex.Message.Contains("基础连接已经关闭")) { throw new Exception("DownFile:" + ex.Message + "|" + url); } } finally { if (myStream != null) { myStream.Close(); } if (FStream != null) { FStream.Close(); } } }
//通过HTTP下载文件 public static bool DownloadFile(string url, string localFileName) { bool flag = false; //打开上次下载的文件 long SPosition = 0; //实例化流对象 FileStream FStream; //判断要下载的文件夹是否存在 if (File.Exists(localFileName)) { //打开要下载的文件 FStream = File.OpenWrite(localFileName); //获取已经下载的长度 SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); } else { //文件不保存创建一个文件 FStream = new FileStream(localFileName, FileMode.Create); SPosition = 0; } try { //打开网络连接 HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url); if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); myRequest.ProtocolVersion = HttpVersion.Version11; myRequest.UserAgent = DefaultUserAgent; } if (SPosition > 0) { myRequest.AddRange((int)SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 WebResponse response = (HttpWebResponse)myRequest.GetResponse(); Stream myStream = response.GetResponseStream(); //定义一个字节数据 byte[] btContent = new byte[512]; int intSize = 0; intSize = myStream.Read(btContent, 0, 512); while (intSize > 0) { FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, 512); } //关闭流 FStream.Close(); myStream.Close(); flag = true; //返回true下载成功 } catch (Exception e) { MessageBox.Show(e.Message); FStream.Close(); flag = false; //返回false下载失败 } return(flag); }
/// <summary> /// 从台账服务器拿db文件http的方式 /// </summary> /// <param name="infoString"></param> /// <returns></returns> public static bool TZDown(string strFileName, string TZdz, string ftplj) { //将文件名改一下 strFileName = strFileName + ".wait"; bool flag = false; //打开上次下载的文件 long SPosition = 0; //实例化流对象 FileStream FStream; //判断要下载的文件夹是否存在 if (File.Exists(strFileName)) { //打开要下载的文件 FStream = File.OpenWrite(strFileName); //获取已经下载的长度 SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); } else { //文件不保存创建一个文件 FStream = new FileStream(strFileName, FileMode.Create); SPosition = 0; } try { //打开网络连接 HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(TZdz); if (SPosition > 0) { myRequest.AddRange((int)SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 Stream myStream = myRequest.GetResponse().GetResponseStream(); //定义一个字节数据 byte[] btContent = new byte[1024]; int intSize = 0; intSize = myStream.Read(btContent, 0, 1024); while (intSize > 0) { FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, 1024); } FStream.Flush(); //关闭流 FStream.Close(); myStream.Close(); //将文件名改回来 string tempName = strFileName; strFileName = strFileName.Substring(0, strFileName.Length - 5); File.Move(tempName, strFileName); flag = true; //返回true下载成功 } catch (Exception ex) { PublicMethod.write_state(ftplj + "\\", -1.01); CYZFramework.Log.CYZLog.writeLog(ex.ToString()); FStream.Close(); flag = false; //返回false下载失败 } return(flag); }
public static bool DownLoad_OffLine(string fileId, string UpLoadFileName, string DownloadFileName, string saveDir, int upSize, DataRow drmission) { bool flag = false; string uri = SysParams.FileServer + SysParams.FileServer_tmpFile + "/" + fileId + "/" + UpLoadFileName; //截取文件名 string fileName = UpLoadFileName; //构造文件完全限定名,准备将网络流下载为本地文件 string fileFullName = saveDir + DownloadFileName + SysParams.tmpDownLoadName; //打开上次下载的文件 long SPosition = 0; //实例化流对象 FileStream FStream; //判断要下载的文件夹是否存在 if (File.Exists(fileFullName)) { //打开要下载的文件 FStream = File.OpenWrite(fileFullName); //获取已经下载的长度 SPosition = FStream.Length; FStream.Seek(SPosition, SeekOrigin.Current); } else { //文件不保存创建一个文件 FStream = new FileStream(fileFullName, FileMode.Create); SPosition = 0; } try { //打开网络连接 HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(uri); if (SPosition > 0) { myRequest.AddRange((int)SPosition); //设置Range值 } //向服务器请求,获得服务器的回应数据流 Stream myStream = myRequest.GetResponse().GetResponseStream(); //定义一个字节数据 byte[] btContent = new byte[upSize]; int intSize = 0; intSize = myStream.Read(btContent, 0, upSize); while (intSize > 0) { FStream.Write(btContent, 0, intSize); intSize = myStream.Read(btContent, 0, upSize); lock (drmission.Table)//更新已上传量 { drmission["UploadSize"] = FStream.Length; } } //关闭流 FStream.Close(); myStream.Close(); flag = true; //返回true下载成功 } catch (Exception) { FStream.Close(); flag = false; //返回false下载失败 } return(flag); }