您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在c#中利用WebRequest實(shí)現(xiàn)一個(gè)多文件上傳功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
添加引用
使用WebRequest需要添加引用System.Web,否則引入出錯(cuò)。
方便起見,我把請(qǐng)求參數(shù)進(jìn)行了封裝,代碼如下:
namespace EasyHttp.Net.Core { public class KeyValue { public string Key; public string Value; public string FilePath; public string ContentType="*/*"; public KeyValue(string key, string value, string filePath, string contentType) { Key = key; Value = value; FilePath = filePath; ContentType = contentType; } public KeyValue() { } public KeyValue(string key, string value, string filePath) { Key = key; Value = value; FilePath = filePath; } public KeyValue(string key, string value) { Key = key; Value = value; } } }
KeyValue代表了廣義的參數(shù),可以是普通的鍵值對(duì),也可以是文件參數(shù)。
public static void ExecuteMultipartRequest(HttpWebRequest request, List<KeyValue> nvc) { Console.WriteLine(request.Headers); // log.Debug(string.Format("Uploading {0} to {1}", file, url)); string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); HttpWebRequest wr = request; wr.ContentType = "multipart/form-data; boundary=" + boundary; wr.Method = "POST"; wr.KeepAlive = true; wr.Credentials = System.Net.CredentialCache.DefaultCredentials; using (var rs = wr.GetRequestStream()) { // 普通參數(shù)模板 string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; //帶文件的參數(shù)模板 string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; foreach (KeyValue keyValue in nvc) { //如果是普通參數(shù) if (keyValue.FilePath == null) { rs.Write(boundarybytes, 0, boundarybytes.Length); string formitem = string.Format(formdataTemplate, keyValue.Key, keyValue.Value); byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); rs.Write(formitembytes, 0, formitembytes.Length); } //如果是文件參數(shù),則上傳文件 else { rs.Write(boundarybytes, 0, boundarybytes.Length); string header = string.Format(headerTemplate, keyValue.Key, keyValue.FilePath, keyValue.ContentType); byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); rs.Write(headerbytes, 0, headerbytes.Length); using (var fileStream = new FileStream(keyValue.FilePath, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[4096]; int bytesRead = 0; long total = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { rs.Write(buffer, 0, bytesRead); total += bytesRead; } } } } byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); rs.Write(trailer, 0, trailer.Length); } }
static void Main(string[] args) { var request = WebRequest.Create("http://localhost:8080/test/upload") as HttpWebRequest; List<KeyValue> keyValues = new List<KeyValue>(); keyValues.Add(new KeyValue("key1","param1")); keyValues.Add(new KeyValue("key2", "param2")); keyValues.Add(new KeyValue("file","test1.png","image/png")); keyValues.Add(new KeyValue("file", "test2.png", "image/png")); EasyHttp.ExecuteMultipartRequest(request,keyValues); }
上述內(nèi)容就是怎么在c#中利用WebRequest實(shí)現(xiàn)一個(gè)多文件上傳功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。