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

后端 未结 6 761
梦谈多话
梦谈多话 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:35

    There is no way to upload a file from Appservice you need to create a web Api controler with a particular method for this action.

      public class EntityImageController : AbpApiController
     {
          private IEntityImageAppService iEntityAppService;
    
          public EntityImageController( IEntityImageAppService pEntityImgAppService ) : base()
          {
               this.LocalizationSourceName = AppConsts.LocalizationSourceName;
               this.iEntityImgAppService = pEntityImgAppService;
          }          
    
          [AbpAuthorize( PermissionNames.Entity_Update )]
          [HttpPost]
          public async Task Set()
          {
    
               // Check if the request contains multipart/form-data.
               if( !Request.Content.IsMimeMultipartContent() )
               {
                    throw new HttpResponseException( HttpStatusCode.UnsupportedMediaType );
               }
    
               string root = HttpContext.Current.Server.MapPath( "~/App_Data" );
               var provider = new MultipartFormDataStreamProvider( root );
    
               try
               {
                    // Read the form data.
                    await Request.Content.ReadAsMultipartAsync( provider );
    
                    var mEntityId = provider.FormData[ "EntityId" ];
    
                    MultipartFileData mFileData = provider.FileData.FirstOrDefault();
                    var mFileInfo = new FileInfo( mFileData.LocalFileName );
                    var mImageBytes = File.ReadAllBytes( mFileInfo.FullName );
    
                    await this.iEntityImgAppService.Set( new EntityImageInput
                    {
                         ImageInfo = mImageBytes,
                         EntityId = Convert.ToInt32( mEntityId )
                    } );
    
                    return Request.CreateResponse( HttpStatusCode.OK );
               }
               catch( System.Exception e )
               {
                    return Request.CreateErrorResponse( HttpStatusCode.InternalServerError, e );
               }
          }
    

    }

提交回复
热议问题