這是我從資料庫中檢索影像并將其顯示給用戶的代碼。
然后它將在另一個空白選項卡上打開并查看。
現在我想知道,我想從控制器下載檔案,而不是在另一個選項卡上查看附件。
如何下載檔案?
這是我的 HTML 代碼
@Html.ActionLink(item.File_Name, "PaymentAttachmentView", "TaskMains", new { id = item.Id }, new { @target = "_blank" })
這是我在控制器中的代碼
public ActionResult RetrieveTaskImage(int id)
{
var q = from c in db.TaskFiles where c.Id == id select c.Attachment;
var type = from t in db.TaskFiles where t.Id == id select t.File_Name;
string fileType = type.First().ToString();
string ext = Path.GetExtension(fileType);
byte[] cover = q.First();
if (cover != null)
{
if (ext == ".pdf")
{
return File(cover, "application/pdf");
}
else
{
return File(cover, "image/jpg");
}
}
else
{
return null;
}
}
uj5u.com熱心網友回復:
您可以嘗試以下方式:
方式:1
控制器:
public async Task<IActionResult> DownloadImage(string imageName)
{
var path = Path.GetFullPath("./wwwroot/ImageName/Cover/" imageName);
MemoryStream memory = new MemoryStream();
using (FileStream stream = new FileStream(path, FileMode.Open))
{
await stream.CopyToAsync(memory);
}
memory.Position = 0;
return File(memory, "image/png", Path.GetFileName(path));
}
下載鏈接 HTML:
<a asp-controller="Application" asp-action="DownloadImage" class="btn btn-info" asp-route-imageName="@item.ImageName">Download</a>
輸出:

注意:它不需要任何Nuget package
方式:2 當您想從資料庫模型在瀏覽器上顯示 PDF 時
解決方案:
public ActionResult DisplayPdfOnBrowserFromDatabaseList()
{
var data = _context.Members.ToList();
var pdf = data.ToPdf();
MemoryStream ms = new MemoryStream(pdf);
return new FileStreamResult(ms, "application/pdf");
}
注意:要處理這種情況,您需要使用
方式:3 當您想在瀏覽器上從資料庫模型或現有檔案下載 PDF 時
解決方案:
public ActionResult DownloadPDFOnBrowser()
{
var data = _context.Members.ToList();
var byteArray = data.ToPdf();
MemoryStream stream = new MemoryStream(byteArray);
string mimeType = "application/pdf";
return new FileStreamResult(stream, mimeType)
{
FileDownloadName = "DatabaseListToPdf.pdf"
};
}
注意:如上所述,我們需要using ArrayToPdf;在頂部添加。要處理這種情況,您需要使用
uj5u.com熱心網友回復:
將第三個引數添加fileDownloadName到 File 方法,這指定將出現下載的名稱。
return File(cover, "application/pdf", "MyFile.pdf");
在幕后,這將Content-Disposition使用檔案名和附件指令設定標題,以告訴瀏覽器下載而不是顯示行內。
uj5u.com熱心網友回復:
您也可以嘗試以下操作:
這是我的控制器動作:
public async Task<FileResult> DownloadMyFiles(string filetype)
{
if (string.IsNullOrEmpty(filetype))
{
return null;
}
var folderPath = Server.MapPath("/Files");
var filePath = String.Empty;
switch (filetype)
{
case "pdf":
filePath = Path.Combine(folderPath, "mypdffile.pdf");
byte[] bytes = System.IO.File.ReadAllBytes(filePath);
return File(bytes, "application/octet-stream", "mypdf.pdf");
case "excel":
filePath = Path.Combine(folderPath, "myexcelfile.xlsx");
byte[] excel_bytes = System.IO.File.ReadAllBytes(filePath);
return File(excel_bytes, "application/octet-stream", "myexcelfile.xlsx");
case "jpg":
filePath = Path.Combine(folderPath, "myjpg.jpg");
byte[] image_bytes = System.IO.File.ReadAllBytes(filePath);
return File(image_bytes, "image/png", "myjpg.jpg");
default:
return null;
}
}
下面是我的 HTML 代碼:
@Html.ActionLink("Download PDF File", "DownloadMyFiles", "Home", new { filetype = "pdf" }, null)
@Html.ActionLink("Download Excel File File", "DownloadMyFiles", "Home", new { filetype = "excel" }, null)
@Html.ActionLink("Download Image File", "DownloadMyFiles", "Home", new { filetype = "jpg" }, null)
請參閱隨附的輸出影像希望我的回答對您有所幫助。:) 謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/525343.html
標籤:C#asp.net 核心asp.net-core-mvc
