1. 程式人生 > 其它 >C# 上傳檔案新增其他引數

C# 上傳檔案新增其他引數

content-Type: multipart/form-data

boundary=ceshi(分組)

請求體中是按照一定格式拼的串,大致就是--ceshi ****--ceshi****--ceshi****--ceshi--這樣的格式。

因為最終是傳送請求流,這些請求體中的內容都需要寫進請求流中,雖然有些事字串這種文字型別的資料,但是檔案本身是二進位制的,所以我就把所有文字都變成了二進位制再加上檔案本身

然後按照這種格式的順序寫入請求流中,最後傳送出去(如果不變成二進位制,則只能進行文字型別的檔案上傳,限制很多,並且容易損壞檔案)。

程式碼如下:

把C盤下的2.html檔案傳送了出去(圖片、pdf 等型別也是可用的,因為都是轉成了二進位制)已經過測試

private void post1()
        {
            string url = @"http://*******";//這裡就不暴露我們的地址啦
            string modelId = "4f1e2e3d-6231-4b13-96a4-835e5af10394";
            string updateTime = "2016-11-03 14:17:25";
            string encrypt = "f933797503d6e2c36762428a280e0559";

            string filePath = @"C:/2.html";
            string fileName = "2.html";

            byte[] fileContentByte = new byte[1024]; // 檔案內容二進位制

            #region 將檔案轉成二進位制

            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            fileContentByte = new byte[fs.Length]; // 二進位制檔案
            fs.Read(fileContentByte, 0, Convert.ToInt32(fs.Length));
            fs.Close();

            #endregion


            #region 定義請求體中的內容 並轉成二進位制

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

            string modelIdStr = "--" + boundary + Enter
                    + "Content-Disposition: form-data; name=\"modelId\"" + Enter + Enter
                    + modelId + Enter;

            string fileContentStr = "--" + boundary + Enter
                    + "Content-Type:application/octet-stream" + Enter
                    + "Content-Disposition: form-data; name=\"fileContent\"; filename=\"" + fileName + "\"" + Enter + Enter;

            string updateTimeStr = Enter + "--" + boundary + Enter
                    + "Content-Disposition: form-data; name=\"updateTime\"" + Enter + Enter
                    + updateTime;

            string encryptStr = Enter + "--" + boundary + Enter
                    + "Content-Disposition: form-data; name=\"encrypt\"" + Enter + Enter
                    + encrypt + Enter + "--" + boundary + "--";


            var modelIdStrByte = Encoding.UTF8.GetBytes(modelIdStr);//modelId所有字串二進位制

            var fileContentStrByte = Encoding.UTF8.GetBytes(fileContentStr);//fileContent一些名稱等資訊的二進位制(不包含檔案本身)

            var updateTimeStrByte = Encoding.UTF8.GetBytes(updateTimeStr);//updateTime所有字串二進位制
            
            var encryptStrByte = Encoding.UTF8.GetBytes(encryptStr);//encrypt所有字串二進位制


            #endregion


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "multipart/form-data;boundary=" + boundary; 

            Stream myRequestStream = request.GetRequestStream();//定義請求流

            #region 將各個二進位制 安順序寫入請求流 modelIdStr -> (fileContentStr + fileContent) -> uodateTimeStr -> encryptStr

            myRequestStream.Write(modelIdStrByte, 0, modelIdStrByte.Length);

            myRequestStream.Write(fileContentStrByte, 0, fileContentStrByte.Length);
            myRequestStream.Write(fileContentByte, 0, fileContentByte.Length);

            myRequestStream.Write(updateTimeStrByte, 0, updateTimeStrByte.Length);

            myRequestStream.Write(encryptStrByte, 0, encryptStrByte.Length);

            #endregion

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();//傳送

            Stream myResponseStream = response.GetResponseStream();//獲取返回值
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

            string retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myResponseStream.Close();
        }