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

这一生的挚爱 提交于 2020-01-21 09:13:07

问题


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

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

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?


回答1:


There is no way to do that via the ApplicationServices. You need to do that in the Web project with an MVC controller and action, like you would do it in a regular project. If you need more help, I can assist.




回答2:


All application services have access to the HttpContext object, so they are also able to handle file upload. To upload a file to an app. service make sure to use the correct url + headers.

App. service example:

public class MyUploadService : MyApplicationBaseClass, IMyUploadService
{
    /// <summary>
    /// References the logging service.
    /// </summary>
    private readonly ILogger _logger;

    /// <summary>
    /// Construct an new instance of this application service.
    /// </summary>
    public MyUploadService(ILogger logger)
    {
        _logger = logger;
    }

    /// <summary>
    /// Method used to handle HTTP posted files.
    /// </summary>
    public async Task Upload()
    {
        // Checking if files are sent to this app service.
        if(HttpContext.Current.Request.Files.Count == 0) 
            throw new UserFriendlyException("No files given.");

        // Processing each given file.
        foreach(var file in HttpContext.Current.Request.Files)
        {
            // Reading out meta info.
            var fileName = file.fileName;
            var extension = Path.GetExtension(file.FileName);

            // Storing file on disk.
            file.SaveAs("~/uploads");
        }
    }
}



回答3:


We implement file upload method : - use sql server fileTable technology - implement application method's service to accept data similar to this Dto

 public FileDto File { get; set; }

...

public class FileDto
{
    public FileDto()
    {

    }
    public FileDto(string file, string fileType)
    {
        File = file;
        FileType = fileType;
    }
    [DisableAuditing]
    public string File { get; set; }
    public string FileName { get; set; }
    public Guid? StreamId { get; set; }
    public string FileType { get; set; } = "";
    [DisableAuditing]
    public string FileWithHeader
    {
        get
        {
            if (FileType == null|| FileType=="")
                return "";
            if (FileType.ToLower() == "jpg" || FileType.ToLower() == "jpeg")
                return "data:image/Jpeg;base64," + File;
            if (FileType.ToLower() == "png")
                return "data:image/png;base64," + File;
            if (FileType.ToLower() == "gif")
                return "data:image/gif;base64," + File;
            if (FileType.ToLower() == "ppt")
                return "data:application/vnd.ms-powerpoint;base64," + File;
            if (FileType.ToLower() == "xls")
                return "data:application/vnd.ms-excel;base64," + File;
            if (FileType.ToLower() == "doc")
                return "data:application/msword;base64," + File;
            if (FileType.ToLower() == "zip")
                return "data:application/zip;base64," + File;
            if (FileType.ToLower() == "exe")
                return "data:application/octet-stream;base64," + File;
            if (FileType.ToLower() == "txt")
                return "data:text/plain;base64," + File;
            if (FileType.ToLower() == "pdf")
                return "data:application/pdf;base64," + File;
            if (FileType.ToLower() == "bmp")
                return "data:image/bmp;base64," + File;
            if (FileType.ToLower() == "csv")
                return "data:text/csv;base64," + File;
            if (FileType.ToLower() == "pptx")
                return "data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64," + File;
            if (FileType.ToLower() == "xlsx")
                return "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," + File;
            if (FileType.ToLower() == "docx")
                return "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64," + File;
            if (FileType.ToLower() == "rar")
                return "data:application/x-rar-compressed;base64," + File;
            if (FileType.ToLower() == "rtf")
                return "data:application/rtf;base64," + File;
            return "";
        }
    }
}

and method implementation

 public UploadFileOutput UploadFile(UploadFileInput input)
    {
        if (input.Id <= 0 || input.File == null)
        {
            throw new UserFriendlyException(L("GetDataError"));
        }

        return new UploadFileOutput()
        {

            StreamId = _attachmentRepo.InsertAttachment(input.File.FileName, Convert.FromBase64String(input.File.File), LIMSConsts.HeightThumbnail, true, ServerConfig.ThumbnailImagePath),
        };

    }

insertAttachment method:

public Guid InsertAttachment(string fileName, byte[] fileStream, int heightThumbnail = 50, bool saveThumbnail = false,string thumbLocationPath=null)
    {

        Guid AttachmentId = Guid.Empty;

        SqlParameter _attachmentId = new SqlParameter();
        _attachmentId.ParameterName = "@attachmentId";
        _attachmentId.Direction = System.Data.ParameterDirection.InputOutput;
        _attachmentId.Value = AttachmentId;

        SqlParameter _fileStream = new SqlParameter();
        _fileStream.SqlDbType = System.Data.SqlDbType.VarBinary;
        _fileStream.Value = fileStream;
        _fileStream.ParameterName = "@fileStream";

        Context.Database.ExecuteSqlCommand("EXECUTE dbo.sp_AttachmentFile_Insert @AttachmentId OUTPUT,@fileName,@fileStream",
            _attachmentId,
            new SqlParameter("@fileName", fileName),
            _fileStream);

        if (saveThumbnail == true) {
            var ms = new MemoryStream(fileStream);
            var image = Image.FromStream(ms);

            var width = (int)(heightThumbnail * image.Width / image.Height);
            var height = (int)(heightThumbnail);

            var Thumbnail = new Bitmap(width, height);
            Graphics.FromImage(Thumbnail).DrawImage(image, 0, 0, width, height);                
            Bitmap Thumb= new Bitmap(Thumbnail);
            Thumb.Save(Path.Combine(thumbLocationPath, _attachmentId.Value+".jpg"), ImageFormat.Jpeg);

        }

        return (Guid)_attachmentId.Value;
    }

and Stored procedure implementation

ALTER PROC [dbo].[sp_AttachmentFile_Insert]
@AttachmentId uniqueidentifier out,
@fileName nvarchar(256),
@fileStream VARBINARY(max)

AS SET @AttachmentId=NEWID()

WHILE(EXISTS(SELECT 1 FROM dbo.Attachment WHERE name=@fileName))
BEGIN
    DECLARE @fileExtention NVARCHAR(100)
    SELECT @fileExtention ='.'+dbo.GetFileExtension(@fileName)
    SET @fileName=REPLACE(@fileName,@fileExtention,'')+
        CAST(DATEPART( DAY, GETDATE()) AS VARCHAR(10))+'_'
        +CAST(DATEPART( HOUR, GETDATE()) AS VARCHAR(10))+'_'+
        +CAST(DATEPART( MINUTE, GETDATE()) AS VARCHAR(10))+'_'+
        +CAST(DATEPART( SECOND, GETDATE()) AS VARCHAR(10))+@fileExtention
END

INSERT into dbo.Attachment(stream_id,name,file_stream)
    VALUES(@AttachmentId,@fileName,@fileStream)

and finally Thank to "Halil İbrahim Kalkan" for thihs awesome framework.




回答4:


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

}



来源:https://stackoverflow.com/questions/46294091/how-do-you-do-file-upload-method-in-appservices-for-aspnetboilerplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!