我将PDF文件放在不同的(文件服务器)服务器上,并且托管MVC应用程序的IIS计算机有权访问该文件服务器。在IIS机器上,我可以通过以下URI访问该文件:
file://file-server/data-folder/pdf/19450205.pdf我想让我的MVC应用程序的用户通过点击下载链接或按钮来下载他们各自的文件。因此,我可能需要为该链接/按钮编写一些Action。
我尝试通过以下方式为我的Action方法使用File返回类型:
public ActionResult FileDownload()
{
string filePth = @"file://file-server/data-folder/pdf/19450205.pdf";
return File(filePth , "application/pdf");
}但是上面的代码给出了不支持URI的异常。
我还尝试使用FileStream读取数组中的字节,返回下载的字节,但FileStream也给出了错误的“虚拟路径”,因为文件没有放在虚拟路径中,它在单独的服务器上。
发布于 2016-04-27 11:07:11
public ActionResult Download()
{
var document = = @"file://file-server/data-folder/pdf/19450205.pdf";
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
FileName = document.FileName,
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(document.Data, document.ContentType);
}发布于 2016-04-28 02:01:18
谢谢你的回复,但这两个建议都不起作用。
由于需要通过URI访问文件,因此使用FileInfo会出现错误:不支持URI格式。
我通过以下机制做到了这一点:
public ActionResult FaxFileDownload()
{
string filePth = @"file://file-server/data-folder/pdf/19450205.pdf";
WebClient wc = new WebClient();
Stream s = wc.OpenRead(filePth);
return File(s, "application/pdf");
}感谢所有人。
https://stackoverflow.com/questions/36879400
复制相似问题