I am trying to upload file from desktop client to server using api. In our project, we are using Refit to create api.
I am trying to implement it according to Refit manual. https://github.com/reactiveui/refit#multipart-uploads
This is Api interface method:
[Multipart]
[Post("/Projects/SaveProjectFile")]
Task<IApiResponse> SaveFileAsync([AliasAs("projectFile")] StreamPart stream, [Query] string projectId);
This is how I use it in code:
public async Task SaveProjectToCloudAsync()
{
using (var fileStream = new FileStream(_pdProject.currentFilePath, FileMode.Open))
{
var streamPart = new StreamPart(fileStream, "project.pd");
var response = await _projectApi.SaveFileAsync(streamPart, _projectToSave.Id);
}
}
And this is my api controller (It is not fished. I am just testing getting a file):
[HttpPost]
public async Task<IActionResult> SaveProjectFileAsync(IFormFile projectFile, [FromQuery] string projectId)
{
await using var saveFileStream = System.IO.File.OpenWrite($"{projectId}_{DateTime.Now.ToString().Replace(":",".")}.pd");
await projectFile.CopyToAsync(saveFileStream);
return Ok();
}
And my data gets to the controller. The problem is: during debug I can see that the server receives IFormFile with correct name of the file (project.pd), but there is no data. Length is zero. As a result it saves an empty file.
I cannot understand what am I doing wrong here, and googling did not help. I would highly appreciate any help you can give me.