0

I'm currently working on a project that uses ASP.NET Boilerplate framework and I'm starting to get the hang of using DTOs.

So I'm trying to implement an upload function where I can upload files/images, but using DTO instead of directly calling it from the controller. I would like some guidance on how to create it from DTO, including AppServices and the JavaScript, if anyone could help me.

aaron
  • 39,695
  • 6
  • 46
  • 102
Lynn
  • 1
  • 2

1 Answers1

2

You have to use IFormFile as a parameter of your service method. Please find the sample code to use it. You can also create a Dto and declare an IFormFile property in Dto class and pass as a parameter to the method.

using Microsoft.AspNetCore.Http;

public async void UploadFile(IFormFile csvInput)
{
    using (var stream = csvInput.OpenReadStream())
    {
        var currentLine = 0;
        using (var reader = new StreamReader(stream))
        {
        ...
        }
    }

}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • Besides IFormFile, is there any other way for me to upload files/images? – Lynn Apr 03 '18 at 06:39
  • Whats problem with `IFormFile`? – Vivek Nuna Apr 03 '18 at 07:06
  • Honestly, I really don't know how to implement that. Like where do I place that? Is it in the app service or straight to controller? – Lynn Apr 03 '18 at 08:27
  • in the appservice, you need to define the method like `UploadFile(IFormFile Input)` – Vivek Nuna Apr 03 '18 at 08:29
  • I can't use IFormFile because I'm not using Asp.NET Core rather I'm using Asp.NET Boilerplate MVC. I tried importing like what you gave but it can't detect because I'm not using Asp.NET Core. Is there any other way for me to make this possible? – Lynn Apr 05 '18 at 07:03
  • @Lynn use `HttpPostedFile` instead – highboi Jan 25 '19 at 19:08