I have a function which adds data to a SQL Database and upload an image(s) to the server. I convert the image into base64 string to send it with my post but I read that the image size is 33% bigger than the original file. I would like to send the post like multipart form data, I don't know how can I receive the data in my model!!
This is my code:
public async Task<IHttpActionResult> Post([FromBody]Post userpost) { if (ModelState.IsValid) { int postid = Convert.ToInt32(postDB.AddPost(userpost)); if (userpost.fileBody.Length > 0) { var filename = string.Format("{0}_{1}{2}", postid, Guid.NewGuid().ToString(), ".jpg"); var ms = new MemoryStream(Convert.FromBase64String(userpost.fileBody)); await upload.UploadFile(filename, ms, "image/jpg","images"); ms.Close(); return Content(HttpStatusCode.Accepted, "{\"pic\":\"" + filename + "\"}"); } else return Ok(HttpStatusCode.OK); } else return Ok(HttpStatusCode.NotAcceptable); }
Thank you.