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

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

    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
    {
        /// 
        /// References the logging service.
        /// 
        private readonly ILogger _logger;
    
        /// 
        /// Construct an new instance of this application service.
        /// 
        public MyUploadService(ILogger logger)
        {
            _logger = logger;
        }
    
        /// 
        /// Method used to handle HTTP posted files.
        /// 
        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");
            }
        }
    }
    

提交回复
热议问题