HTTPPOST 方式上传, 一次 HTTP 请求,需要同时发送两个文件

首先,在Content-Type中,boundary这个属性一定要加,但boundary属性值随意设置成什么都行,它不会出现在要上传的数据里,只是起到分隔的作用。重点来了:每个参数之前需要加上“--boundary”,注意前面有“--”。参数最后结尾要加“--boundary--”。文件数据后要跟上“\r\n”(我就是因为没加“\r\n,所以没有文件后缀”)

 

 public static string FileLoadRequest(string url,string XmlName,string XmlPath,string PdfName,string PdfPath)
        {

            string boundary = "ceshi";
            string Enter = "\r\n";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "multipart/form-data;charset=utf8;boundary=" + boundary;  //boundary这个属性很重要


            #region 将参数写入请求流中
            Stream RequestStream = request.GetRequestStream();
            string fileContentStr = "--" + boundary + Enter
                    + "Content-Type:application/octet-stream" + Enter
                    + "Content-Disposition: form-data; name=\"ReportInfo\"; filename=\"" + XmlName + "\"" + Enter + Enter;
            var fileContentStrByte = Encoding.UTF8.GetBytes(fileContentStr);

            FileStream fs = new FileStream(XmlPath, FileMode.Open, FileAccess.Read);
            byte[] filedata = new byte[fs.Length];
            fs.Read(filedata, 0, Convert.ToInt32(fs.Length));
            fs.Close();

            RequestStream.Write(fileContentStrByte, 0, fileContentStrByte.Length);
            RequestStream.Write(filedata, 0, filedata.Length);


            //在文件数据末尾要加上“\r\n”
            string EndStr1 = Enter;
            var EndStr1Byte = Encoding.UTF8.GetBytes(EndStr1);
            RequestStream.Write(EndStr1Byte, 0, EndStr1Byte.Length);



            string fileContentStr1 = "--" + boundary + Enter
                    + "Content-Type:application/octet-stream" + Enter
                    + "Content-Disposition: form-data; name=\"Report\"; filename=\"" + PdfName + "\"" + Enter + Enter;
            var fileContentStrByte1 = Encoding.UTF8.GetBytes(fileContentStr1);

            FileStream fs1 = new FileStream(PdfPath, FileMode.Open, FileAccess.Read);
            byte[] filedata1 = new byte[fs1.Length];
            fs1.Read(filedata1, 0, Convert.ToInt32(fs1.Length));
            fs1.Close();

            RequestStream.Write(fileContentStrByte1, 0, fileContentStrByte1.Length);
            RequestStream.Write(filedata1, 0, filedata1.Length);



            //在流末尾要加上--ceshi--的分隔符
            string EndStr = Enter + "--" + boundary + "--";
            var EndStrByte = Encoding.UTF8.GetBytes(EndStr);
            RequestStream.Write(EndStrByte, 0, EndStrByte.Length);
            #endregion



            RequestStream.Flush();
            RequestStream.Close();

            //返回响应
            HttpWebResponse myResponse;
            try
            {
                myResponse = (HttpWebResponse)request.GetResponse();

            }
            catch (WebException er)
            {

                return er.StackTrace;
            }
            if (myResponse != null && myResponse.StatusCode == HttpStatusCode.OK)
            {
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

                var str = reader.ReadToEnd();
                return str;
            }
            else
            {
                return "请求报错";
            }

            return "";
        }

 

你可能感兴趣的:(http文件上传,学习区4【ASP.Net,C#】)