B
B
BernadetteWolowitz2016-03-01 16:20:12
ASP.NET
BernadetteWolowitz, 2016-03-01 16:20:12

How to get byte[] passed from client in asp.net 5 web api controller?

I want to pass a byte[] and a string to the controller. For this I use MultipartFormDataContent. But I can't get the transferred array of bytes in the controller. In this case, the string can be obtained.
Client code:

public static void UploadFile()
        {
            Uri uploadEndpoint = new Uri("http://localhost:56010/api/values");
            var content = new MultipartFormDataContent();

            content.Add(new StringContent("any string"), "anystring");
            var fileContent = new ByteArrayContent(new byte[]{3,3,3,3});
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("anybytes")
            {
                FileName = "Foo.txt"
            };
            content.Add(fileContent);

            var client = new HttpClient();
            HttpResponseMessage hrm = client.PostAsync(uploadEndpoint, content).Result;
}

In the Controller, the AnyBytes parameter comes null. And I would like to see your array.
[HttpPost]
        public IActionResult PostFile(byte[] anybytes)
        {
               //здесь anybytes == null, а должен быть переданный массив
                return new HttpOkResult();

        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
asdz, 2016-03-01
@asdz

www.stackoverflow.com/a/23884972

F
Fredcapit, 2016-03-10
@Fredcapit

I recommend to get acquainted with MediaTypeFormatter.
A cool thing when you need to conveniently get the data right away in the right form.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question