Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HttpContent into byte[]

I am currently working on a c# web API. For a specific call I need to send 2 images using an ajax call to the API, so that the API can save them as varbinary(max) in the database.

  1. How do you extract an Image or byte[] from a HttpContent object?
  2. How do I do this twice? Once for each image.

-

var authToken = $("#AuthToken").val();
var formData = new FormData($('form')[0]);
debugger;
$.ajax({
    url: "/api/obj/Create/", 
    headers: { "Authorization-Token": authToken },
    type: 'POST',
    xhr: function () { 
        var myXhr = $.ajaxSettings.xhr();
        return myXhr;
    },
    data: formData,
    cache: false,
    contentType: false,
    processData: false
});

-

public async Task<int> Create(HttpContent content)
{
    if (!content.IsMimeMultipartContent())
    {
        throw new UnsupportedMediaTypeException("MIME Multipart Content is not supported");
    }

    return 3;
}
like image 234
James Madison Avatar asked Jun 30 '15 19:06

James Madison


1 Answers

HttpContent has a Async method which return ByteArray i.e (Task of ByteArray)

 Byte[] byteArray = await Content.ReadAsByteArrayAsync();

You can run the method synchronously

Byte[] byteArray = Content.ReadAsByteArrayAsync().Result;
like image 73
ClearLogic Avatar answered Oct 26 '22 04:10

ClearLogic