Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Post JSON Data and image at the same time to a web Api 2

$
0
0

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.


Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>