-
-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: Add a sample showing how to do a file upload #170
Comments
Any update on this? Do you know where I can find an example? |
@jonathanjameswilliams26 did you manage to find a way to do the file upload? |
@belavenuto Yeah I was able to use the public record UploadFileRequest
{
[FromForm]
public IFormFile File { get; set; }
} |
Not worked to me ...someone can help me ? |
@renannn it worked for me. This is my endpoint:
The handle method:
The DTO:
|
Why would it work any differently than on a normal controller? Using the sample, for example: using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Mvc;
namespace SampleEndpointApp.Endpoints.Authors;
public class File : EndpointBaseAsync
.WithRequest<IFormFile>
.WithResult<ActionResult<string[]>>
{
/// <summary>
/// Post author's photo or something
/// </summary>
[HttpPost("api/[namespace]/file")]
public override async Task<ActionResult<string[]>> HandleAsync(
IFormFile file,
CancellationToken cancellationToken = default)
{
string filePath = Path.GetTempFileName();
using (var fileStream = System.IO.File.Create(filePath))
{
await file.CopyToAsync(fileStream, cancellationToken);
}
return new[]
{
filePath,
file.FileName,
file.ContentType,
file.ContentDisposition,
file.Length.ToString()
};
}
} Works OK with Swagger and Postman. Model binding docs: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-7.0. File upload docs (read this if you want streaming instead of buffering): https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-7.0. |
Thanks @mariusz96 ! |
No description provided.
The text was updated successfully, but these errors were encountered: