public HttpResponseMessage DownloadFile([FromBody] FileDownloadModel fileObject) { HttpResponseMessage response; CaseTransferService service = new CaseTransferService(); try { string inputfilePath = Encoding.UTF8.GetString(Convert.FromBase64String(fileObject.FilePath)); string basePathID = Encoding.UTF8.GetString(Convert.FromBase64String(fileObject.BasePathId)); string filePath = string.Empty; if (fileObject.UseCustomBasePath) { string customPhysicalBasePath = Encoding.UTF8.GetString(Convert.FromBase64String(fileObject.CustomPhysicalBasePath)); filePath = customPhysicalBasePath + inputfilePath; } else { filePath = service.GetPhysicalBasePath("PhysicalBasePath", Convert.ToString(basePathID)) + inputfilePath; } if (File.Exists(filePath)) { var byteArray = File.ReadAllBytes(filePath); var dataStream = new MemoryStream(byteArray); response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StreamContent(dataStream); response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = Path.GetFileName(inputfilePath); response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); } else { response = Request.CreateResponse(HttpStatusCode.BadRequest); } return(response); } catch (Exception ex) { log.Error("Error: " + ex.Message + ex.StackTrace); throw ex; } }
static public FileDownloadModel DownloadFile(FileDTO file) { if (file != null) { var download = new FileDownloadModel() { Array = System.IO.File.ReadAllBytesAsync((file.Path.Link)).Result, Name = file.Name + file.Type.Format, Type = "application/" + file.Type.Format }; //todo :Ex return(download != null ? download : throw new Exception()); } else { //todo :Ex throw new Exception(); } }
public async Task <FileDownloadModel> DownloadBlob(string blobName) { var container = BlobHelper.GetBlobContainer(); var blob = container.GetBlockBlobReference(blobName); var ms = new MemoryStream(); await blob.DownloadToStreamAsync(ms); var lastPos = blob.Name.LastIndexOf('/'); var fileName = blob.Name.Substring(lastPos + 1, blob.Name.Length - lastPos - 1); var downloadFileModel = new FileDownloadModel { BlobStream = ms, BlobFileName = fileName, BlobLength = blob.Properties.Length, BlobContentType = blob.Properties.ContentType }; return(downloadFileModel); }
public async Task <IActionResult> Upload(IList <IFormFile> files) { try { if (files.Any(f => !Path.GetExtension(f.FileName).Equals(".pdf"))) { return(Redirect("~/Home")); } if (files.Any(f => f.Length > 31457280)) { return(Redirect("~/Home")); } FileDownloadModel fdm = new FileDownloadModel(); fdm.Files = new List <FileModel>(); long size = files.Sum(f => f.Length); long totalReadBytes = 0; string firstFolderId = Guid.NewGuid().ToString(); string directoryPath = $"{this.HostingEnvironment.WebRootPath}/download/{firstFolderId}"; var filePath = Path.GetTempFileName(); filePath = filePath.Replace(".tmp", ".pdf"); foreach (var formFile in files) { var type = Path.GetExtension(formFile.FileName); if (formFile.Length > 0) { byte[] buffer = new byte[16 * 1024]; using (var stream = new FileStream(filePath, FileMode.Create)) { using (Stream input = formFile.OpenReadStream()) { int readBytes; while ((readBytes = input.Read(buffer, 0, buffer.Length)) > 0) { await stream.WriteAsync(buffer, 0, readBytes); } } await formFile.CopyToAsync(stream); } directoryPath = string.Concat(directoryPath, "/" + formFile.FileName); Directory.CreateDirectory(directoryPath); using (var rasterizer = new GhostscriptRasterizer()) { rasterizer.Open(filePath); for (int i = 1; i <= rasterizer.PageCount; i++) { var page = rasterizer.GetPage(120, 120, i); var savePath = $"{directoryPath}/{formFile.FileName}_{i}.jpg"; page.Save(savePath); } rasterizer.Close(); } string zipPath = $"{this.HostingEnvironment.WebRootPath}/download/{firstFolderId}/{formFile.FileName}.zip"; ZipFile.CreateFromDirectory(directoryPath, zipPath); FileModel fileModel = new FileModel { FileName = formFile.FileName, FilePath = zipPath }; fdm.Files.Add(fileModel); } directoryPath = $"{this.HostingEnvironment.WebRootPath}/download/{firstFolderId}"; } fdm.DownloadAllId = firstFolderId; if (files.Count > 1) { string allZipPath = $"{this.HostingEnvironment.WebRootPath}/download/{firstFolderId}/pdftojpg.zip"; var allToZip = Directory.GetFiles(directoryPath); using (ZipArchive newFile = ZipFile.Open(allZipPath, ZipArchiveMode.Create)) { foreach (string file in allToZip) { newFile.CreateEntryFromFile(file, System.IO.Path.GetFileName(file)); } } } return(View(fdm)); } catch (Exception ex) { log.Error(ex.Message, ex); return(Redirect("~/Home")); } }
public ActionResult DownloadFile(Guid?id) { FileDownloadModel inv = this.associateService.GetInvoiceFile(null, id); return(FileStreamHelper.FileDownloadModelResult(inv, this.HttpContext, true)); }