问题描述
我真的很喜欢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层中编写接收文件上传的方法?关于粗俗有很好的例子,并且大张旗鼓地把它们弄清楚.现在我要实现文件上传.甚至可以在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?
推荐答案
无法通过Appservice上传文件,您需要使用此操作的特定方法创建Web Api控件.
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<HttpResponseMessage> 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 );
}
}
}
这篇关于您如何在AppServices中为aspnetboilerplate进行文件上传方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!