public async Task <DownLoadModel> DownLoadFileAsync(string fileId)
        {
            try
            {
                DownLoadModel model = new DownLoadModel();

                var asset = await GetById(fileId);

                if (asset == null)
                {
                    return(null);
                }

                model.ContentType = GetContentType(asset.Path);
                model.FileName    = asset.Name;
                model.FilePath    = Path.Combine(this.storageAccount.BlobEndpoint.AbsoluteUri, asset.Path);

                return(model);
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex.Message);
                return(null);
            }
        }
Exemple #2
0
        public async Task <DownLoadModel> DownLoadFileAsync(string fileId)
        {
            try
            {
                DownLoadModel model = new DownLoadModel();

                var asset = await GetById(fileId);

                if (asset == null)
                {
                    return(null);
                }

                model.ContentType = GetContentType(asset.Path);
                model.FileName    = asset.Name;

                model.Memory = new MemoryStream();
                using (var stream = new FileStream(asset.Path, FileMode.Open))
                {
                    await stream.CopyToAsync(model.Memory);
                }
                model.Memory.Position = 0;

                return(model);
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex.Message);
                return(null);
            }
        }
        public async Task <ActionResult> Upload()
        {
            try
            {
                var file = Request.Form.Files[0];

                var fileId = await _assetService.UploadFileAsync(file);

                if (fileId == null)
                {
                    return(BadRequest());
                }

                DownLoadModel model = await _assetService.DownLoadFileAsync(fileId);

                var result = new
                {
                    //location = "assets/download/" + fileId + ""
                    location = model.FilePath
                };
                return(Ok(result));
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex.Message);
                return(BadRequest());
            }
        }
        public async Task <ActionResult> Download(string id)
        {
            try
            {
                if (id == null)
                {
                    return(Content("filename not present"));
                }

                /* ========== Return file implementation
                 *
                 * var downLoadModel = await _assetService.DownLoadFile(id);
                 *
                 * if (downLoadModel == null)
                 *  return NotFound();
                 *
                 *
                 * return File(downLoadModel.Memory, downLoadModel.ContentType, downLoadModel.FileName);
                 */


                var storageType = this._configuration["storage:storageType"];
                switch (storageType)
                {
                case "static":
                {
                    var file = await _assetService.GetById(id);

                    var filePath = Path.Combine(Directory.GetCurrentDirectory(),
                                                "wwwroot", "Upload", file.Name);

                    var contentType = _assetService.GetContentType(file.Path);

                    return(PhysicalFile(filePath, contentType));
                }

                case "azure":
                {
                    DownLoadModel model = await _assetService.DownLoadFileAsync(id);

                    var contentType = _assetService.GetContentType(model.FilePath);

                    return(PhysicalFile(model.FilePath, contentType));
                }

                default:
                    return(null);
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex.Message);
                return(BadRequest());
            }
        }