Reputation: 981
I am trying to upload a file (image) from a WPF application to a Web Api controller. In the controller I convert the file into a bit array and save it in a DB. I used the following code for sending files to web api
var client = new WebClient();
client.UploadFile("URI", "POST", "filepath");
In my web api I am checking if the incoming request is MimemultipartContent
if (Request.Content.IsMimeMultipartContent())
This works fine. But when I try to send a data buffer instead of a file I am stuck how to write my server side code.
var bytes = File.ReadAllBytes('filepath');
client.UploadData("URI", "POST", bytes);
Upvotes: 2
Views: 2803
Reputation: 981
Got it. It's simple actually.
var task = Request.Content.ReadAsByteArrayAsync();
var bytes = task.Result;
Image img = new Image();
img.Id = Guid.NewGuid();
img.ImageData = bytes;
db.Images.Add(img);
db.SaveChanges();
Upvotes: 1