<ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>

      系列目錄???? 【已更新最新開(kāi)發(fā)文章,點(diǎn)擊查看詳細(xì)】
      <https://www.cnblogs.com/SavionZhang/p/11422481.html>
      應(yīng)用程序中上傳附件是最常使用的操作之一,ASP.NET客戶端一般通過(guò)上傳控件實(shí)現(xiàn),
      <input type="file" id="fileUpload" runat="server" />
      后臺(tái)C#使用以下方式將文件保存到服務(wù)上
      1 HttpFileCollection files = HttpContext.Current.Request.Files; 2
      HttpPostedFile postedFile = files["fileUpload"]; 3
      postedFile.SaveAs(postedFile.FileName);
      上述的場(chǎng)景是簡(jiǎn)單的管理系統(tǒng)與網(wǎng)站中最常用的方式將客戶端的文件上傳到IIS服務(wù)器的指定目錄下。

      隨著云端應(yīng)用的發(fā)展與普及,第三方應(yīng)用平臺(tái)或者開(kāi)發(fā)平臺(tái)部署在云服務(wù)器上,例如阿里云、騰訊云、七牛云、青云等。第三方對(duì)外開(kāi)放的應(yīng)用平臺(tái)大都是提供Restful
      API供開(kāi)發(fā)者調(diào)用以上傳(本地或者遠(yuǎn)端文件)或下載業(yè)務(wù)數(shù)據(jù)進(jìn)行業(yè)務(wù)開(kāi)發(fā)。

      傳統(tǒng)應(yīng)用程序的上傳控件方式在云端應(yīng)用程序中針對(duì)附件上傳與下載完全不適用。

      下面提供一種通用的上傳附件的方式:
      1 /// <summary> 2 /// 將數(shù)據(jù)緩沖區(qū)(一般是指文件流或內(nèi)存流對(duì)應(yīng)的字節(jié)數(shù)組)上載到由 URI 標(biāo)識(shí)的資源。(包含body數(shù)據(jù)) 3
      /// </summary> 4 /// <param name="url">請(qǐng)求目標(biāo)URL</param> 5 /// <param
      name="data">主體數(shù)據(jù)(字節(jié)數(shù)據(jù))</param> 6 /// <param name="method">請(qǐng)求的方法。請(qǐng)使用
      WebRequestMethods.Http 的枚舉值</param> 7 /// <param name="contentType"><see
      langword="Content-type" /> HTTP 標(biāo)頭的值。請(qǐng)使用 ContentType 類的常量來(lái)獲取。默認(rèn)為
      application/octet-stream</param> 8 /// <returns>HTTP-POST的響應(yīng)結(jié)果</returns> 9
      public HttpResult UploadData(string url, byte[] data, string method =
      WebRequestMethods.Http.Post,string contentType =
      HttpContentType.APPLICATION_OCTET_STREAM)10 { 11 HttpResult httpResult = new
      HttpResult();12 HttpWebRequest httpWebRequest = null; 13 14 try 15 { 16
      httpWebRequest = WebRequest.Create(url)as HttpWebRequest; 17
      httpWebRequest.Method = method; 18 httpWebRequest.Headers = HeaderCollection; 19
      httpWebRequest.CookieContainer = CookieContainer; 20
      httpWebRequest.ContentLength = data.Length; 21 httpWebRequest.ContentType =
      contentType;22 httpWebRequest.UserAgent = _userAgent; 23
      httpWebRequest.AllowAutoRedirect = _allowAutoRedirect; 24
      httpWebRequest.ServicePoint.Expect100Continue =false; 25 26 if (data != null) 27
      {28 httpWebRequest.AllowWriteStreamBuffering = true; 29 using (Stream
      requestStream = httpWebRequest.GetRequestStream()) 30 { 31
      requestStream.Write(data,0, data.Length); 32 requestStream.Flush(); 33 } 34 }
      35 36 HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as
      HttpWebResponse;37 if (httpWebResponse != null) 38 { 39 GetResponse(ref
      httpResult, httpWebResponse);40 httpWebResponse.Close(); 41 } 42 } 43 catch
      (WebException webException)44 { 45 GetWebExceptionResponse(ref httpResult,
      webException);46 } 47 catch (Exception ex) 48 { 49 GetExceptionResponse(ref
      httpResult, ex, method, contentType);50 } 51 finally 52 { 53 if
      (httpWebRequest !=null) 54 { 55 httpWebRequest.Abort(); 56 } 57 } 58 59
      return httpResult; 60 }
      借助該方法,又衍生出一下2中重載方式:

      重載1:將指定的本地文件上載到具有指定的 URI 的資源。(包含body數(shù)據(jù))
      1 /// <summary> 2 /// 將指定的本地文件上載到具有指定的 URI 的資源。(包含body數(shù)據(jù)) 3 /// </summary>
      4 /// <param name="url">請(qǐng)求目標(biāo)URL</param> 5 /// <param name="fileFullName">
      待上傳的文件(包含全路徑的完全限定名)</param> 6 /// <param name="method">請(qǐng)求的方法。請(qǐng)使用
      WebRequestMethods.Http 的枚舉值</param> 7 /// <param name="contentType"><see
      langword="Content-type" /> HTTP 標(biāo)頭的值。請(qǐng)使用 ContentType 類的常量來(lái)獲取。默認(rèn)為
      application/octet-stream</param> 8 /// <returns>HTTP-POST的響應(yīng)結(jié)果</returns> 9
      public HttpResult UploadFile(string url, string fileFullName, string method =
      WebRequestMethods.Http.Post,string contentType =
      HttpContentType.APPLICATION_OCTET_STREAM)10 { 11 HttpResult httpResult = new
      HttpResult();12 if (!File.Exists(fileFullName)) 13 { 14 httpResult.Status =
      HttpResult.STATUS_FAIL;15 16 httpResult.RefCode = (int
      )HttpStatusCode2.USER_FILE_NOT_EXISTS;17 httpResult.RefText =
      HttpStatusCode2.USER_FILE_NOT_EXISTS.GetCustomAttributeDescription();18 } 19
      else 20 { 21 FileStream fileStream = new FileStream(fileFullName,
      FileMode.Open, FileAccess.Read);22 byte[] data = fileStream.ToByteArray(); 23
      httpResult = UploadData(url, data, method, contentType); 24 } 25 26 return
      httpResult;27 }
      重載2:?將指定的數(shù)據(jù)流對(duì)象(一般指文件流或內(nèi)存流)上載到具有指定的 URI 的資源。(包含body數(shù)據(jù))
      1 /// <summary> 2 /// 將指定的數(shù)據(jù)流對(duì)象(一般指文件流或內(nèi)存流)上載到具有指定的 URI 的資源。(包含body數(shù)據(jù)) 3 ///
      </summary> 4 /// <param name="url">請(qǐng)求目標(biāo)URL</param> 5 /// <param name="stream">
      一般指文件流或內(nèi)存流</param> 6 /// <param name="method">請(qǐng)求的方法。請(qǐng)使用 WebRequestMethods.Http
      的枚舉值</param> 7 /// <param name="contentType"><see langword="Content-type" />
      HTTP 標(biāo)頭的值。請(qǐng)使用 ContentType 類的常量來(lái)獲取。默認(rèn)為 application/octet-stream</param> 8 ///
      <returns>HTTP-POST的響應(yīng)結(jié)果</returns> 9 public HttpResult UploadStream(string url,
      Stream stream,string method = WebRequestMethods.Http.Post, string contentType =
      HttpContentType.APPLICATION_OCTET_STREAM)10 { 11 HttpResult httpResult = new
      HttpResult();12 if (stream == null) 13 { 14 httpResult.Status =
      HttpResult.STATUS_FAIL;15 16 httpResult.RefCode = (int
      )HttpStatusCode2.USER_STREAM_NULL;17 httpResult.RefText =
      HttpStatusCode2.USER_STREAM_NULL.GetCustomAttributeDescription();18 } 19 else
      20 { 21 byte[] data = stream.ToByteArray(); 22 httpResult = UploadData(url,
      data, method, contentType);23 } 24 25 return httpResult; 26 }
      其中?UploadData()?調(diào)用了
      GetResponse()、GetWebExceptionResponse()、GetExceptionResponse()方法
      1 /// <summary> 2 /// 獲取HTTP的響應(yīng)信息 3 /// </summary> 4 /// <param
      name="httpResult">即將被HTTP請(qǐng)求封裝函數(shù)返回的HttpResult變量</param> 5 /// <param
      name="httpWebResponse">正在被讀取的HTTP響應(yīng)</param> 6 private void GetResponse(ref
      HttpResult httpResult, HttpWebResponse httpWebResponse) 7 { 8
      httpResult.HttpWebResponse = httpWebResponse; 9 httpResult.Status =
      HttpResult.STATUS_SUCCESS;10 httpResult.StatusDescription =
      httpWebResponse.StatusDescription;11 httpResult.StatusCode = (int
      )httpWebResponse.StatusCode;12 13 if (ReadMode == ResponseReadMode.Binary) 14 {
      15 int len = (int)httpWebResponse.ContentLength; 16 httpResult.Data = new byte
      [len];17 int bytesLeft = len; 18 int bytesRead = 0; 19 20 using (BinaryReader
      br =new BinaryReader(httpWebResponse.GetResponseStream())) 21 { 22 while
      (bytesLeft >0) 23 { 24 bytesRead = br.Read(httpResult.Data, len - bytesLeft,
      bytesLeft);25 bytesLeft -= bytesRead; 26 } 27 } 28 } 29 else 30 { 31 using
      (StreamReader sr =new StreamReader(httpWebResponse.GetResponseStream())) 32 {
      33 httpResult.Text = sr.ReadToEnd(); 34 } 35 } 36 } 1 /// <summary> 2 ///
      獲取HTTP訪問(wèn)網(wǎng)絡(luò)期間發(fā)生錯(cuò)誤時(shí)引發(fā)的異常響應(yīng)信息 3 /// </summary> 4 /// <param name="httpResult">
      即將被HTTP請(qǐng)求封裝函數(shù)返回的HttpResult變量</param> 5 /// <param name="webException">
      訪問(wèn)網(wǎng)絡(luò)期間發(fā)生錯(cuò)誤時(shí)引發(fā)的異常對(duì)象</param> 6 private void GetWebExceptionResponse(ref
      HttpResult httpResult, WebException webException) 7 { 8 HttpWebResponse
      exResponse = webException.Responseas HttpWebResponse; 9 if (exResponse != null)
      10 { 11 httpResult.HttpWebResponse = exResponse; 12 httpResult.Status =
      HttpResult.STATUS_FAIL;13 httpResult.StatusDescription =
      exResponse.StatusDescription;14 httpResult.StatusCode = (int
      )exResponse.StatusCode;15 16 httpResult.RefCode = httpResult.StatusCode; 17
      using (StreamReader sr = new StreamReader(exResponse.GetResponseStream(),
      EncodingType))18 { 19 httpResult.Text = sr.ReadToEnd(); 20 httpResult.RefText =
      httpResult.Text;21 } 22 23 exResponse.Close(); 24 } 25 } /// <summary> ///
      獲取HTTP的異常響應(yīng)信息/// </summary> /// <param name="httpResult">
      即將被HTTP請(qǐng)求封裝函數(shù)返回的HttpResult變量</param> /// <param name="ex">異常對(duì)象</param> ///
      <param name="method">HTTP請(qǐng)求的方式</param> /// <param name="contentType">HTTP的標(biāo)頭類型
      </param> private void GetExceptionResponse(ref HttpResult httpResult, Exception
      ex,string method, string contentType = "") { contentType = string
      .IsNullOrWhiteSpace(contentType) ?string.Empty : "-" + contentType;
      StringBuilder sb= new StringBuilder(); sb.AppendFormat("[{0}] [{1}] [HTTP-" +
      method + contentType +"] Error: ", DateTime.Now.ToString("yyyy-MM-dd
      HH:mm:ss.ffff"), _userAgent); Exception exception = ex; while (exception != null
      ) { sb.AppendLine(exception.Message+ " "); exception =
      exception.InnerException; } sb.AppendLine(); httpResult.HttpWebResponse= null;
      httpResult.Status= HttpResult.STATUS_FAIL; httpResult.RefCode = (int
      )HttpStatusCode2.USER_UNDEF; httpResult.RefText+= sb.ToString(); }
      ?
      系列目錄???? 【已更新最新開(kāi)發(fā)文章,點(diǎn)擊查看詳細(xì)】
      <https://www.cnblogs.com/SavionZhang/p/11422481.html>

      友情鏈接
      ioDraw流程圖
      API參考文檔
      OK工具箱
      云服務(wù)器優(yōu)惠
      阿里云優(yōu)惠券
      騰訊云優(yōu)惠券
      京東云優(yōu)惠券
      站點(diǎn)信息
      問(wèn)題反饋
      郵箱:[email protected]
      QQ群:637538335
      關(guān)注微信

        <ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>
          欧美超碰人人操 | 国产精品乱码久久 | 扯下内裤粗大挤进去好涨 | 娇妻在粗大的胯下受辱 | 无码一区二区区 | 永久免费无码A片在线观看大豆网 | free国产性白嫩白嫩xxxx | 无码人妻一区二区三区免费九色 | 小舞揉搓难受3d动漫 | 欧美做爰BBB性BBBBB丨D |