Submitting data using multipart/form-data is different from ordinary post methods. The request header of multipart/form-data must contain a special header: Content-Type, whose value must be multipart/form-data. In addition, it is also necessary to specify a content splitter to split the content of multiple posts in the request body, such as file content and text content. Only in this way can the server parse the data normally. However, the basis of multipart/form-data is still post, which is implemented by the post method. The following are two methods of submitting multipart/form-data data.
1. Submit data using form
<form action="" method="post" enctype="multipart/form-data"> <input type="text" name="uname" class="uname" /><br /> <input type="text" name="email" class="email" /><br /> <input type="file" name="file" class="file" /><br /> <input type="submit" name="submit" value="submit"/> </form>
Two ways to submit data in form form.
(1) application/x-www-form-urlencoded cannot be used to upload files, you can only submit text. Of course, if there is a file control, you can only submit file names.
(2) multipart/form-data is used to upload files.
2. Use HttpClient and MultipartFormDataContent
using (var client = new HttpClient()) using (var content = new MultipartFormDataContent()) { = new Uri("http://localhost/WapAPIExp/"); var fileContent1 = new ByteArrayContent((@"D:\")); = new ContentDispositionHeaderValue("file") { FileName = "" }; var dataContent = new ByteArrayContent(Encoding.("1")); = new ContentDispositionHeaderValue("form") { Name = "type" }; (fileContent1); (dataContent); var result = ("api/Upload", content).Result; }
This is the article about C# using multipart form-data to post data to the server. For more related multipart form-data content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!