question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

File upload using IFormFile at application services

See original GitHub issue

I have a doubt about how to make a file upload to an ApplicationService.

I made two approaches: trying to have an IFormFile inside an input object and trying to have an IFormFile as one of the two arguments in the method.

For instance:

public class SomeObjectInput
 {
     public string AProperty { get; set; }
     public IFormFile File { get; set; }
 }
public class TaskAppService : ApplicationService, ITaskAppService {

     public async Task AMethod(SomeObjectInput input)
     {
            // Code here
     }

}

This approach returns that a IFormFile cannot be instantiated (because it is an interface).

public class TaskAppService : ApplicationService, ITaskAppService {

     public async Task AMethod(IFormFile file, SomeObjectInput input)
     {
            // Code here
     }

}

In this other approach, the file parameter is always null.

Is this not possible at the application service level?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RobersonLuocommented, Dec 12, 2020

I think you all thought too much! The point is is how to get the files from the method of the class XXXAppService which derived from ApplicationService, not from the XXXController which derived from AbpController or Microsoft.AspNetCore.Mvc.Controller. So you should remember the class: HttpContext/HtttpRequest/HttpResponse!!!

solution: inject httpContextAccessor in the class XXXAppService will achive it. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-5.0

Here is my codes, wish will help you!

------Server side (ABP)-------------- [Route(“api/”)] public class XXXAppService : ApplicationService { private readonly IHttpContextAccessor _httpContextAccessor; public XXXAppService(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; }

[HttpPost, Route("upload")]
public void UploadFile()
{
    var files = _httpContextAccessor.HttpContext.Request.Form.Files;
    //do logics as you like here...
}

}

----Client side (Angular/Typescript/PrimeNG)---------------------------------------------- —(1) UI (PrimeNG upload component) <p-fileUpload name=“demo[]” [multiple]=“true” [url]=“uploadUrl” (onUpload)=“onUpload($event)”> <ng-template pTemplate="content"> <ul *ngIf=“uploadedFiles.length”> <li *ngFor=“let file of uploadedFiles”>{{file.name}} - {{file.size}}byte </ng-template> </p-fileUpload>

—(2) UI logic (Angular component) import { AppConsts } from ‘@shared/AppConsts’;

uploadUrl: string = ‘’; uploadedFiles: any[] = []; ngOnInit() { //http://localhost:21021/api/upload let url_ = AppConsts.remoteServiceBaseUrl + “/api/upload”;
this.uploadUrl = url_.replace(/[?&]$/, “”); }

onUpload(event: any) { _.forEach(event.files, v => { this.uploadedFiles.push(v); }); }

0reactions
RobersonLuocommented, Mar 20, 2021

@amendez1000 , I think you need to upload your Images or Files to a 3th Object Storage Service (OSS, like Amazon Simple Storage Service). Then you can load、display or download them on any client in anywhere.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Upload files in ASP.NET Core
Two general approaches for uploading files are buffering and streaming. Buffering. The entire file is read into an IFormFile. IFormFile is a C# ......
Read more >
File Upload in ASP.NET Core 6 - Detailed Guide
In this article, the main focus will be on how we can implement file upload in ASP.NET Core MVC. The IFormFile interface is...
Read more >
Upload Single Or Multiple Files In ASP.NET Core Using ...
In this article, we are going to see how to upload files in asp.net core web application and store them in root directory...
Read more >
How to upload files in ASP.NET Core?
If you want to upload the file to some directory in your app, you should use IHostingEnvironment to get the webroot path.
Read more >
Uploading Files in ASP.NET Core 6 MVC
Http namespace to upload files in our application. The IFormFile interface in Microsoft.AspNetCore.Http namespace represents a file uploaded ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found