Core 2.0 MVC - 如何 link 将动态 PDF 保存在文件系统上
Core 2.0 MVC - How to link to a dynamic PDF saved on the file system
我有一个 .NET Core 2.0 MVC 项目。我的一个模型有一个名为 PDFUpload 的 NotMapped IFormFile 字段,它进行上传并将文件保存到文件系统上根以外的某个位置,该路径保存在 PDF 字段中。当用户点击特定的 URL 时,PDF 需要在浏览器中显示或下载。我的 Details.cshtml 视图目前只显示路径
@Html.DisplayFor(model => model.PDF)
如何将其转换为实际的 link 以提供来自特定 URL 的文件而不显示路径?
更新:这是工作版本
if (System.IO.File.Exists(pdf))
{
var stream = new FileStream(pdf, FileMode.Open);
// send the file to the browser
stream.Position = 0;
//return File(stream,"application/pdf","filename.pdf") //- will force download
return new FileStreamResult(stream, "application/pdf");//will display in browser
}
您可以只执行控制器操作来提供文件。您将需要一些方法来识别文件并使用该标识符来定位它。像这样的东西(我用的是鼻涕虫):
public IActionResult GimmeThePdf(string slug)
{
string filePathToPdf = // look this up in a database given the slug, map the path, etc.
// open the file for reading
using (Stream stream = new FileStream(filePathToPdf, FileMode.Open)
{
// send the file to the browser
return File(stream, "application/pdf");
}
}
那么在你看来:
//generate the link, could also do this with tag helpers
@Html.ActionLink("download the pdf", "GimmeThePdf", "YourController", new {slug = Model.PdfSlug});
这将生成如下内容:
<a href="http://yoursite.com/YourController/GimmeThePdf?slug=...">download the pdf</a>
我只是想添加到@Becuzz 答案中。我在处理我的文件时遇到了问题,我在这里找到了答案 MVC FileStreamResult and handling other
public IActionResult GimmeThePdf(string slug)
{
string filePathToPdf = // look this up in a database given the slug, map the path, etc.
// open the file for reading
FileStream stream = new FileStream(filePathToPdf, FileMode.Open);
// Register this stream for disposal after response is finished.
HttpContext.Response.RegisterForDispose(stream);
// send the file to the browser
//return File(stream,"application/pdf","filename.pdf") //- will force download
return File(stream, "application/pdf");
}
我有一个 .NET Core 2.0 MVC 项目。我的一个模型有一个名为 PDFUpload 的 NotMapped IFormFile 字段,它进行上传并将文件保存到文件系统上根以外的某个位置,该路径保存在 PDF 字段中。当用户点击特定的 URL 时,PDF 需要在浏览器中显示或下载。我的 Details.cshtml 视图目前只显示路径
@Html.DisplayFor(model => model.PDF)
如何将其转换为实际的 link 以提供来自特定 URL 的文件而不显示路径?
更新:这是工作版本
if (System.IO.File.Exists(pdf))
{
var stream = new FileStream(pdf, FileMode.Open);
// send the file to the browser
stream.Position = 0;
//return File(stream,"application/pdf","filename.pdf") //- will force download
return new FileStreamResult(stream, "application/pdf");//will display in browser
}
您可以只执行控制器操作来提供文件。您将需要一些方法来识别文件并使用该标识符来定位它。像这样的东西(我用的是鼻涕虫):
public IActionResult GimmeThePdf(string slug)
{
string filePathToPdf = // look this up in a database given the slug, map the path, etc.
// open the file for reading
using (Stream stream = new FileStream(filePathToPdf, FileMode.Open)
{
// send the file to the browser
return File(stream, "application/pdf");
}
}
那么在你看来:
//generate the link, could also do this with tag helpers
@Html.ActionLink("download the pdf", "GimmeThePdf", "YourController", new {slug = Model.PdfSlug});
这将生成如下内容:
<a href="http://yoursite.com/YourController/GimmeThePdf?slug=...">download the pdf</a>
我只是想添加到@Becuzz 答案中。我在处理我的文件时遇到了问题,我在这里找到了答案 MVC FileStreamResult and handling other
public IActionResult GimmeThePdf(string slug)
{
string filePathToPdf = // look this up in a database given the slug, map the path, etc.
// open the file for reading
FileStream stream = new FileStream(filePathToPdf, FileMode.Open);
// Register this stream for disposal after response is finished.
HttpContext.Response.RegisterForDispose(stream);
// send the file to the browser
//return File(stream,"application/pdf","filename.pdf") //- will force download
return File(stream, "application/pdf");
}