你如何在 AppServices 中为 aspnetboilerplate 执行文件上传方法? [英] How do you do File Upload method in AppServices for aspnetboilerplate?

查看:28
本文介绍了你如何在 AppServices 中为 aspnetboilerplate 执行文件上传方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢aspnetboilerplate框架,我现在正在学习/使用它..

I really like aspnetboilerplate framework, I learning/using it now..

您如何在 AppServices 中为 aspnetboilerplate 执行文件上传"逻辑/方法?角度部分我已经弄清楚并且工作正常.

How do you do 'File Upload' logic/method in AppServices for aspnetboilerplate? The angular part have I figured out and is working fine.

appservice层接收文件上传的方法是怎么写的?有关于 crud 的很好的例子,并且 swagger 把它们弄清楚了.现在我想实现文件上传.这在 appservice 层甚至是可能的,还是我必须在控制器方法中这样做?

How is it intended to write the methods receiving a file upload in the appservice layer? There is good examples on crud, and swagger figures them out. Now I want to implement file upload. Is this even possible in the appservice layer, or do I have to do this in the controller methods?

推荐答案

重点是如何从 ApplicationService 派生的类 XXXAppService 的方法中获取文件,而不是从 AbpController 或 Microsoft.AspNetCore 派生的 XXXController.Mvc.Controller.

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.

所以你应该记住这个类:HttpContext/HtttpRequest/HttpResponse!!!

So you should remember the class: HttpContext/HtttpRequest/HttpResponse!!!

解决方案:在类XXXAppService中注入httpContextAccessor即可.https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-5.0

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!

------服务器端(ABP)--------------

------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...
    }
}

---(1) UI(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 / 1000}}kb</li>
   </ul>
  </ng-template>
</p-fileUpload>

---(2) UI 逻辑(组件)

---(2) UI logic (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);
});

}

这篇关于你如何在 AppServices 中为 aspnetboilerplate 执行文件上传方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆