jkh
jkh

Reputation: 3678

IFormFileCollection is null when adding file to Flurl multi-part POST request

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.

Upvotes: 3

Views: 2285

Answers (1)

Nan Yu
Nan Yu

Reputation: 27538

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 .

Upvotes: 1

Related Questions