关键词搜索

源码搜索 ×
×

C#对接PHP上传文件

发布2022-10-30浏览415次

详情内容

  1. /// <summary>
  2. /// 上传文件
  3. /// </summary>
  4. /// <param name="fileAdress">文件地址</param>
  5. /// <returns></returns>
  6. public responseObject UploadFile(string fileAdress)
  7. {
  8. //服务器地址
  9. string url = "服务器地址";
  10. //post请求地址
  11. string postRequestAddress = "post请求地址";
  12. //请求参数 注意这里请求参数中不包括要上传文件的那个请求参数
  13. Dictionary<string, string> RequestParameters = new Dictionary<string, string>();
  14. RequestParameters.Add("请求参数名1", "请求参数值1");
  15. RequestParameters.Add("请求参数名2", "请求参数值2");
  16. //发送上传文件
  17. string responseStr =HttpPostFile(url + "/api_pc_cashier/" + postRequestAddress, RequestParameters,fileAdress);
  18. //将返回的数据解析为对象
  19. responseObject responseObj = JsonConvert.DeserializeObject<responseObject>(responseStr);
  20. return responseObj;
  21. }
  22. /// <summary>
  23. /// 上传文件
  24. /// </summary>
  25. /// <param name="Url">服务器地址</param>
  26. /// <param name="postParameter">请求参数</param>
  27. /// <param name="fileAdress">文件地址 </param>
  28. /// <param name="isSuccess"></param>
  29. /// <returns></returns>
  30. public static string HttpPostFile(string Url, Dictionary<string, string> RequestParameters, string fileAdress)
  31. {
  32. // 要上传的文件
  33. FileStream fs = new FileStream(fileAdress, FileMode.Open, FileAccess.Read);
  34. BinaryReader r = new BinaryReader(fs);
  35. //时间戳做分隔符
  36. string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
  37. byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n"); //请求头部信息
  38. StringBuilder sb = new StringBuilder();
  39. //构造POST请求体
  40. foreach (KeyValuePair<string, string> item in RequestParameters)
  41. {
  42. sb.Append("--");
  43. sb.Append(strBoundary);
  44. sb.Append("\r\n")
  45. .Append("Content-Disposition: form-data; name=\"")
  46. .Append(item.Key + "\"").Append("\r\n")
  47. .Append("\r\n").Append(item.Value).Append("\r\n");
  48. }
  49. sb.Append("--");
  50. sb.Append(strBoundary);
  51. sb.Append("\r\n");
  52. sb.Append("Content-Disposition: form-data; name=\"");
  53. sb.Append("imgFile");//注意这里是你请求参数中要上传的文件对应的请求参数名称,
  54. sb.Append("\"; filename=\"");
  55. sb.Append(fileAdress);
  56. sb.Append("\";");
  57. sb.Append("\r\n");
  58. sb.Append("Content-Type: ");
  59. sb.Append("application/octet-stream");
  60. sb.Append("\r\n");
  61. sb.Append("\r\n");
  62. string strPostHeader = sb.ToString();
  63. byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader); // 根据uri创建HttpWebRequest对象
  64. HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(Url));
  65. httpReq.Method = "POST"; //对发送的数据不使用缓存
  66. httpReq.AllowWriteStreamBuffering = false; //设置获得响应的超时时间(300秒)
  67. httpReq.Timeout = 300000;
  68. httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
  69. long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length;
  70. //long fileLength = fs.Length;
  71. httpReq.ContentLength = length;
  72. try
  73. {
  74. //每次上传4k
  75. int bufferLength = 4096;
  76. byte[] buffer = new byte[bufferLength]; //已上传的字节数
  77. long offset = 0; //开始上传时间
  78. DateTime startTime = DateTime.Now;
  79. int size = r.Read(buffer, 0, bufferLength);
  80. Stream postStream = httpReq.GetRequestStream(); //发送请求头部消息
  81. postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
  82. while (size > 0)
  83. {
  84. postStream.Write(buffer, 0, size);
  85. offset += size;
  86. TimeSpan span = DateTime.Now - startTime;
  87. double second = span.TotalSeconds;
  88. size = r.Read(buffer, 0, bufferLength);
  89. }
  90. //添加尾部的时间戳
  91. postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
  92. postStream.Close();
  93. //获取服务器端的响应
  94. WebResponse webRespon = httpReq.GetResponse();
  95. Stream s = webRespon.GetResponseStream();
  96. //读取服务器端返回的消息
  97. StreamReader sr = new StreamReader(s);
  98. String sReturnString = sr.ReadLine();
  99. return sReturnString;
  100. }
  101. catch(WebException ex)
  102. {
  103. HttpWebResponse res = (HttpWebResponse)ex.Response;
  104. return ex.Message;
  105. }
  106. finally
  107. {
  108. fs.Close();
  109. r.Close();
  110. }
  111. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载