Skip to content
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

Closed
ardalis opened this issue Jan 17, 2022 · 7 comments
Closed

Docs: Add a sample showing how to do a file upload #170

ardalis opened this issue Jan 17, 2022 · 7 comments

Comments

@ardalis
Copy link
Owner

ardalis commented Jan 17, 2022

No description provided.

@jonowilliams26
Copy link

Any update on this? Do you know where I can find an example?

@belavenuto
Copy link

@jonathanjameswilliams26 did you manage to find a way to do the file upload?

@jonowilliams26
Copy link

@belavenuto Yeah I was able to use the [FromForm] attribute on my request dto.

public record UploadFileRequest
{
      [FromForm]
      public IFormFile File { get; set; }
}

And this is what I had to use in postman
image

@renannn
Copy link

renannn commented Sep 13, 2022

Not worked to me ...someone can help me ?

@belavenuto
Copy link

@renannn it worked for me. This is my endpoint:

public class PurchaseOrderReadXml : EndpointBaseAsync .WithRequest<PurchaseOrderUploadXmlRequest> .WithActionResult<PurchaseOrderDto>

The handle method:

public override async Task<ActionResult<PurchaseOrderDto>> HandleAsync( [FromForm] PurchaseOrderUploadXmlRequest request, CancellationToken cancellationToken) {

The DTO:

public class PurchaseOrderUploadXmlRequest { [FromForm] public IFormFile? File { get; set; } }

@mariusz96
Copy link

mariusz96 commented Oct 4, 2023

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.

@ardalis
Copy link
Owner Author

ardalis commented Oct 5, 2023

Thanks @mariusz96 !

@ardalis ardalis closed this as completed Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants