Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Upload files via web api, the files are sent as byte[]

DannyBOAHDannyBOAH Member ✭✭
edited February 6 in Xamarin.Forms

I'm trying to upload files via web api, the files are sent as byte[].

I manage to upload only one file per request, but if I select multiple files it only upload one file.

This is the client side code:

var content = new MultipartFormDataContent();
ByteArrayContent byteContent = new ByteArrayContent(_mediaFile);
content.Add(byteContent, "file", _mediaFIleName);
var httpClient = new HttpClient();
var uploadServiceBaseAddress = "http://localhost:1000/api/home/Upload";
var httpResponseMessage = httpClient.PostAsync(uploadServiceBaseAddress, content);

This is the server side code:

var httpRequest = HttpContext.Current.Request;
foreach (string file in httpRequest.Files)
{
     var postedFile = httpRequest.Files[file];
     var filePath = HttpContext.Current.Server.MapPath("~/uploads" + postedFile.FileName);
     postedFile.SaveAs(filePath);
}

Is there another method to do this or am I doing something wrong here in the code above?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    You need to retrieve the file list and add single file as HttpContent to MultipartFormDataContent .

    Please refer to https://stackoverflow.com/a/60665222/8187800 .

    Also check CrossGeeks/FileUploaderPlugin , it supports multiple file picking and uploading .


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • DannyBOAHDannyBOAH Member ✭✭

    Thanks for your reply,

    I do not understand how this is actually done from what you sent to me.

    Because I have 2 buttons, one to select the images and the other to upload the images.
    So I understand from you that you expect me to keep the selection on list, but how do I move that list to the upload action?

  • ColeXColeX Member, Xamarin Team Xamurai
    edited February 8

    Use jamesmontemagno/MediaPlugin to picker multiple images from gallery , PickPhotosAsync method will return a List of MediaFile.

Sign In or Register to comment.