File upload using IFormFile at application services
See original GitHub issueI 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:
- Created 3 years ago
- Comments:15 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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; }
}
----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); }); }
@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.