Exemplo n.º 1
0
 /// <summary>
 /// 触发上传进度事件。
 /// </summary>
 /// <param name="e"></param>
 protected virtual void OnUploadProgress(ProgressEventArgs e)
 {
     UploadProgressEventHandler handler = this.UploadProgress;
     if (handler != null)
     {
         handler(e);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// 将数据流上传到指定的服务器(HTTP)方式。
 /// </summary>
 /// <param name="input">上传文件流。</param>
 /// <param name="saveName">文件上传后的名称</param>
 /// <returns>结果,成功为true,失败返回0</returns>
 public bool Upload(Stream input, string saveName)
 {
     bool result = false;
     if (input == null)
     {
         throw new ArgumentNullException("fs");
     }
     if (string.IsNullOrEmpty(this.URL))
     {
         throw new ArgumentNullException("未设置文件上传到的服务器地址(URL)!");
     }
     string strBoundary = this.BuildBoundary();
     byte[] boundaryBytes = this.BuildBoundary(strBoundary);
     byte[] postHeaderBytes = this.BuildPostHeader(strBoundary, saveName);
     //根据URI创建HttpWebRequest对象。
     HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(this.URL));
     httpReq.Method = "POST";
     //对发送方数据不使用缓存。
     httpReq.AllowWriteStreamBuffering = false;
     httpReq.Timeout = this.Timeout;
     httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
     long fileLength = input.Length;
     long length = fileLength + postHeaderBytes.Length + boundaryBytes.Length;
     httpReq.ContentLength = length;
     try
     {
         byte[] buffer = new byte[4096];
         using (BinaryReader reader = new BinaryReader(input))
         {
             using (Stream post = httpReq.GetRequestStream())
             {
                 DateTime startTime = DateTime.Now;
                 long offset = 0; int count = 0;
                 ProgressEventArgs args = null;
                 //发送请求头部消息。
                 post.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                 //上传数据。
                 while ((count = reader.Read(buffer, 0, buffer.Length)) > 0)
                 {
                     post.Write(buffer, 0, count);
                     offset += count;
                     args = new ProgressEventArgs();
                     args.Value = (int)(offset * (int.MaxValue / length));
                     args.Time = (DateTime.Now - startTime).TotalSeconds;
                     args.TimeText = string.Format("已用时:{0:F2}秒", args.Time);
                     if (args.Time > 0.01)
                     {
                         args.SpeedText = "平均速度:" + (offset / 1024 / args.Time).ToString("0.00") + "KB/秒";
                     }
                     else
                     {
                         args.SpeedText = "正在连接...";
                     }
                     args.Status = string.Format("{0:F2}%,{1:F2}M/{2:F2}M", (offset * 100.0 / length), (offset / CONST_BYTESTOM), (fileLength / CONST_BYTESTOM));
                     this.OnUploadProgress(args);
                 }
                 //添加尾部时间戳。
                 post.Write(boundaryBytes, 0, boundaryBytes.Length);
                 post.Close();
             }
             reader.Close();
         }
         //获取服务器响应。
         using (WebResponse resp = httpReq.GetResponse())
         {
             result = this.OnUploadCallback(new CallbackEventArgs(resp.GetResponseStream()));
         }
     }
     finally
     {
         input.Close();
     }
     return result;
 }