ASP.NET How to read data with several parts in web API?

I submit multiparty form data to my web API as follows:

string example = "my string"; HttpContent stringContent = new StringContent(example); HttpContent fileStreamContent = new StreamContent(stream); using (var client = new HttpClient()) { using (var content = new MultipartFormDataContent()) { content.Add(stringContent, "example", "example"); content.Add(fileStreamContent, "stream", "stream"); var uri = "http://localhost:58690/api/method"; HttpResponseMessage response = await client.PostAsync(uri, content); 

and this is the web API:

 [HttpPost] [Route("api/method")] public async Task<HttpResponseMessage> Method() { // take contents and do something } 

How to read string and stream from request body in my web API?

+12
c # asp.net-web-api
source share
6 answers

This will help you get started:

  var uploadPath = HostingEnvironment.MapPath("/") + @"/Uploads"; Directory.CreateDirectory(uploadPath); var provider = new MultipartFormDataStreamProvider(uploadPath); await Request.Content.ReadAsMultipartAsync(provider); // Files // foreach (MultipartFileData file in provider.FileData) { Debug.WriteLine(file.Headers.ContentDisposition.FileName); Debug.WriteLine("File path: " + file.LocalFileName); } // Form data // foreach (var key in provider.FormData.AllKeys) { foreach (var val in provider.FormData.GetValues(key)) { Debug.WriteLine(string.Format("{0}: {1}", key, val)); } } 
+10
source share

This is the code I used earlier to get json + data from an additional file:

 var result = await Request.Content.ReadAsMultipartAsync(); var requestJson = await result.Contents[0].ReadAsStringAsync(); var request = JsonConvert.DeserializeObject<MyRequestType>(requestJson); if (result.Contents.Count > 1) { var fileByteArray = await result.Contents[1].ReadAsByteArrayAsync(); ... } 

It is really neat in that you can combine different types of data in a query like this.

Edit: an example of sending this request:

 let serialisedJson = JSON.stringify(anyObject); let formData = new FormData(); formData.append('initializationData', serialisedJson); // fileObject is an instance of File if (fileObject) { // the 'jsonFile' name might cause some confusion: // in this case, the uploaded file is actually a textfile containing json data formData.append('jsonFile', fileObject); } return new Promise((resolve, reject) => { let xhr = new XMLHttpRequest(); xhr.open('POST', 'http://somewhere.com', true); xhr.onload = function(e: any) { if (e.target.status === 200) { resolve(JSON.parse(e.target.response)); } else { reject(JSON.parse(e.target.response)); } }; xhr.send(formData); }); 
+10
source share

You can read the contents and get all the information about the file (in my example image) without copying to the local disk as follows:

 public async Task<IHttpActionResult> UploadFile() { if (!Request.Content.IsMimeMultipartContent()) { return StatusCode(HttpStatusCode.UnsupportedMediaType); } var filesReadToProvider = await Request.Content.ReadAsMultipartAsync(); foreach (var stream in filesReadToProvider.Contents) { // Getting of content as byte[], picture name and picture type var fileBytes = await stream.ReadAsByteArrayAsync(); var pictureName = stream.Headers.ContentDisposition.FileName; var contentType = stream.Headers.ContentType.MediaType; } } 
+1
source share

To send more than one file

  System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files; //// CHECK THE FILE COUNT. for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++) { System.Web.HttpPostedFile hpf = hfc[iCnt]; string Image = UploadDocuments.GetDocumentorfileUri(hpf); UploadDocuments.UploadDocumentsIntoData(Image, hpf.FileName, id); } 

Submitting HTML form data to ASP.NET Web API: file uploads and multi-page MIME

0
source share
  // read the file content without copying to local disk and write the content byte to file try { var filesReadToProvider = await Request.Content.ReadAsMultipartAsync(); JavaScriptSerializer json_serializer = new JavaScriptSerializer(); foreach (var stream in filesReadToProvider.Contents) { //getting of content as byte[], picture name and picture type var fileBytes = await stream.ReadAsByteArrayAsync(); var fileName = stream.Headers.ContentDisposition.Name; var pictureName = stream.Headers.ContentDisposition.FileName; var contentType = stream.Headers.ContentType.MediaType; var path = Path.Combine(HttpContext.Current.Server.MapPath("~/Images/Upload/"), json_serializer.Deserialize<string>(pictureName)); File.WriteAllBytes(path, fileBytes); } return Request.CreateResponse(HttpStatusCode.OK); }catch(Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex); } 
0
source share

// Web UI method is protected void btnPrescAdd_Click (object sender, EventArgs e) {

  NameValueCollection collection = new NameValueCollection(); collection.Set("c1", Session["CredID"].ToString()); collection.Set("p1", ""); collection.Set("p2", Request.Form["ctl00$MainContent$hdnHlthId"]); collection.Set("p3", Request.Form["ctl00$MainContent$PresStartDate"]); collection.Set("p4", Request.Form["ctl00$MainContent$PrescEndDate"]); FileUpload fileUpload = PrescUpload; ApiServices<Status> obj = new ApiServices<Status>(); Status objReturn = obj.FetchObjectUploadAPI("POSTUHRPL", collection, fileUpload, ApiServices<Status>.ControllerType.DU); } 

// Request Method

  public T1 FetchObjectUploadAPI(string strAPIMethod, NameValueCollection collection, FileUpload file, ControllerType enObj) { T1 objReturn; try { string url = strWebAPIUrl + getControllerName(enObj) + strAPIMethod; MultipartFormDataContent content = new MultipartFormDataContent(); int count = collection.Count; List<string> Keys = new List<string>(); List<string> Values = new List<string>(); //MemoryStream filedata = new MemoryStream(file); //Stream stream = filedata; for (int i = 0; i < count; i++) { Keys.Add(collection.AllKeys[i]); Values.Add(collection.Get(i)); } for (int i = 0; i < count; i++) { content.Add(new StringContent(Values[i], Encoding.UTF8, "multipart/form-data"), Keys[i]); } int fileCount = file.PostedFiles.Count(); HttpContent filecontent = new StreamContent(file.PostedFile.InputStream); content.Add(filecontent, "files"); HttpClient client = new HttpClient(); HttpResponseMessage response = client.PostAsync(url, content).Result; if (response.IsSuccessStatusCode) { objReturn = (new JavaScriptSerializer()).Deserialize<T1>(response.Content.ReadAsStringAsync().Result); } else objReturn = default(T1); } catch (Exception ex) { Logger.WriteLog("FetchObjectAPI", ex, log4net_vayam.Constants.levels.ERROR); throw (ex); } return objReturn; } 

https://stackoverflow.com/users/9600164/gaurav-sharma

0
source share

All Articles