How do you do File Upload method in AppServices for aspnetboilerplate?

后端 未结 6 760
梦谈多话
梦谈多话 2021-01-14 11:17

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

How do you do \'File Upload\' logic/method in AppServices for aspnetboilerplate? The angular par

6条回答
  •  情深已故
    2021-01-14 11:50

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

    ---(1) UI (PrimeNG upload component)

    
      
        
    • {{file.name}} - {{file.size / 1000}}kb

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

    }

提交回复
热议问题