3

I am trying to upload a file using Flurl using AddFile.

The resulting IFormFileCollection is null, although I am able to see the item when looking at Request.Form.Files[0] with the right content length.

Creating the request:

public Task<HttpResponseMessage> UploadImage(string fileName, MemoryStream stream)
{
    stream.Position = 0;

    _baseUrl
        .AppendPathSegments("uploadImage")
        .PostMultipartAsync(mp => mp
            .AddFile("files", stream, fileName))
}

Handling the request:

[HttpPost]
[Route("uploadImage")]
public async Task<HttpResponseMessage> UploadImages([FromForm] IFormFileCollection files)
{
    //files is null, but Request.Form.Files[0] in the immediate window shows the file.
}

A common problem seems to be a mismatch in the name of the parameter and the name in the Content-Disposition header, but I updated them to both be files and I am still having the same issue.

jkh
  • 3,618
  • 8
  • 38
  • 66
  • I won't answer because I don't know, but here's a couple things to try: 1) remove the `[FromForm]` attribute, 2) since you're only uploading 1 file in this example, use `IFormFile` instead of `IFormFileCollection`. – Todd Menier Nov 03 '19 at 15:29

1 Answers1

1

That is strange that it works on my side :

MemoryStream ms = new MemoryStream();
using (FileStream file = new FileStream("txt.txt", FileMode.Open, FileAccess.Read))
    file.CopyTo(ms);

ms.Position = 0;
var _baseUrl = "https://localhost:44392/";
var result = await _baseUrl
    .AppendPathSegments("uploadImage")
    .PostMultipartAsync(mp => mp
    .AddFile("files", ms, "txt.txt"));

Result :

enter image description here

Please firstly try with a clean file and use await for handing request .

Community
  • 1
  • 1
Nan Yu
  • 26,101
  • 9
  • 68
  • 148
  • I'm not sure what is different about our two environments, but I tried taking your sample, and `files` still has a count of 0, but `Request.Form.Files[0]` in the immediate window shows my file info with the right filename and length. – jkh Nov 04 '19 at 15:02