2

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?

דניאל
  • 183
  • 9
  • I did not understand exactly what you want but if you did not see this link then see this link link:https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-2. – Meysam Asadi Feb 07 '21 at 09:20
  • Do you want to receive the submitted files from the form? – Meysam Asadi Feb 07 '21 at 09:21
  • If you can tell me exactly what you want it for, maybe I can help – Meysam Asadi Feb 07 '21 at 09:38
  • Thanks for the reply. I will explain to you what I am trying to do with the code above, I have a local website that provides an API service and I want through the API service to upload multiple files by one call to the API. The thing is that I send these files as byte[] and every time the server receives a POST call to upload multiple files it uploads only one file and does not read all the files I selected – דניאל Feb 07 '21 at 11:52
  • You can use a for loop. For example, you send a request number for 5 images. You send 5 images in a loop, like Bluetooth. – Meysam Asadi Feb 07 '21 at 13:10
  • You mean I'll send 5 POST calls to the server for 5 images? – דניאל Feb 07 '21 at 13:21
  • Not . Send one call to the server and the server will send 5 files to the client – Meysam Asadi Feb 07 '21 at 13:23

1 Answers1

1

see this example

[HttpGet]
public IHttpActionResult SendBytes(string input)
{
    string[] paths = input.Split('*');
           
    foreach (var path in paths)
    {
       var content = new MultipartFormDataContent();
       ByteArrayContent byteContent = new ByteArrayContent(File.ReadAllBytes(path));
       content.Add(byteContent, "file", path);
       var httpClient = new HttpClient();
       var uploadServiceBaseAddress = "http://localhost:56381/api/BazarAlborzApp/RecieveBytes";
       var httpResponseMessage = httpClient.PostAsync(uploadServiceBaseAddress, content);
    }
    return Ok<int>(0);
}

[HttpPost]
public IHttpActionResult RecieveBytes()
{       
    var httpRequest = HttpContext.Current.Request;
    foreach (string file in httpRequest.Files)
    {
        var postedFile = httpRequest.Files[file];
        var filePath = Path.Combine(HttpContext.Current.Server.MapPath("/uploads/" + postedFile.FileName));
        postedFile.SaveAs(filePath);
    }
    return Ok<int>(0);
}
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
  • I tried your suggestion and it's return an error that it can't find the file path (System.IO.DirectoryNotFoundException). *I will detail something else that I did not specify before, to upload the files I use a plugin called FilePicker to select multiple images in Xamarin.Forms. – דניאל Feb 07 '21 at 13:49
  • Address error gives the address you need to set. Remove Path.Combine – Meysam Asadi Feb 07 '21 at 14:27
  • It works if the addresses are set correctly – Meysam Asadi Feb 07 '21 at 14:52