Asp.net mvc 4 单击ActionLink时如何打开文件?
当用户单击actionlink时,如何打开服务器上的现有文件?以下代码适用于下载文件,但我想打开一个新的浏览器窗口或选项卡,并显示文件内容Asp.net mvc 4 单击ActionLink时如何打开文件?,asp.net-mvc-4,Asp.net Mvc 4,当用户单击actionlink时,如何打开服务器上的现有文件?以下代码适用于下载文件,但我想打开一个新的浏览器窗口或选项卡,并显示文件内容 public ActionResult Download() { return File(@"~\Files\output.txt", "application/text", "blahblahblah.txt"); } byte[] fileBytes = System.IO.File.ReadAllBytes(contentDetailInfo
public ActionResult Download()
{
return File(@"~\Files\output.txt", "application/text", "blahblahblah.txt");
}
byte[] fileBytes = System.IO.File.ReadAllBytes(contentDetailInfo.ContentFilePath);
Response.AppendHeader("Content-Disposition", "inline; filename=" + contentDetailInfo.ContentFileName);
return File(fileBytes, contentDetailInfo.ContentFileMimeType);
使用链接上的空白目标在新窗口或选项卡中打开它:
<a href="/ControllerName/Download" target="_blank">Download File</a>
byte[] fileBytes = System.IO.File.ReadAllBytes(contentDetailInfo.ContentFilePath);
Response.AppendHeader("Content-Disposition", "inline; filename=" + contentDetailInfo.ContentFileName);
return File(fileBytes, contentDetailInfo.ContentFileMimeType);
但是,强制浏览器显示内容是您无法控制的,因为这完全取决于用户如何配置其浏览器来处理应用程序/文本
文件
byte[] fileBytes = System.IO.File.ReadAllBytes(contentDetailInfo.ContentFilePath);
Response.AppendHeader("Content-Disposition", "inline; filename=" + contentDetailInfo.ContentFileName);
return File(fileBytes, contentDetailInfo.ContentFileMimeType);
如果要处理文本,可以创建一个视图并填充该视图上的文本,然后将其作为常规HTML页面返回给用户。使用
File()
方法的方式是在第三个参数中指定一个文件名,这将导致将内容处理
头发送到客户端。此标题告诉web浏览器响应是要保存的文件(并建议保存该文件的名称)。浏览器可以覆盖此行为,但服务器无法控制此行为
byte[] fileBytes = System.IO.File.ReadAllBytes(contentDetailInfo.ContentFilePath);
Response.AppendHeader("Content-Disposition", "inline; filename=" + contentDetailInfo.ContentFileName);
return File(fileBytes, contentDetailInfo.ContentFileMimeType);
您可以尝试不指定文件名:
return File(@"~\Files\output.txt", "application/text");
byte[] fileBytes = System.IO.File.ReadAllBytes(contentDetailInfo.ContentFilePath);
Response.AppendHeader("Content-Disposition", "inline; filename=" + contentDetailInfo.ContentFileName);
return File(fileBytes, contentDetailInfo.ContentFileMimeType);
响应仍然是一个文件,最终仍取决于浏览器如何处理它。(同样,不能从服务器控制。)从技术上讲,HTTP中没有“文件”这样的东西,它只是响应中的头和内容。通过省略建议的文件名,本例中的框架可能会省略
内容处置
头,这是您想要的结果。值得在浏览器中测试结果,以查看是否确实省略了标题。请尝试此操作,并在html操作链接中替换控制器名称和操作名称
public ActionResult ShowFileInNewTab()
{
using (var client = new WebClient()) //this is to open new webclient with specifice file
{
var buffer = client.DownloadData("~\Files\output.txt");
return File(buffer, "application/text");
}
}
byte[] fileBytes = System.IO.File.ReadAllBytes(contentDetailInfo.ContentFilePath);
Response.AppendHeader("Content-Disposition", "inline; filename=" + contentDetailInfo.ContentFileName);
return File(fileBytes, contentDetailInfo.ContentFileMimeType);
或
byte[] fileBytes = System.IO.File.ReadAllBytes(contentDetailInfo.ContentFilePath);
Response.AppendHeader("Content-Disposition", "inline; filename=" + contentDetailInfo.ContentFileName);
return File(fileBytes, contentDetailInfo.ContentFileMimeType);
这是显示在新空白选项卡中的操作链接
<%=Html.ActionLink("Open File in New Tab", "ShowFileInNewTab","ControllerName", new { target = "_blank" })%>
byte[] fileBytes = System.IO.File.ReadAllBytes(contentDetailInfo.ContentFilePath);
Response.AppendHeader("Content-Disposition", "inline; filename=" + contentDetailInfo.ContentFileName);
return File(fileBytes, contentDetailInfo.ContentFileMimeType);
必须为新选项卡添加“内联”
byte[] fileBytes = System.IO.File.ReadAllBytes(contentDetailInfo.ContentFilePath);
Response.AppendHeader("Content-Disposition", "inline; filename=" + contentDetailInfo.ContentFileName);
return File(fileBytes, contentDetailInfo.ContentFileMimeType);
我不能投你的票,因为你的回答很有用,跟着道。非常感谢
byte[] fileBytes = System.IO.File.ReadAllBytes(contentDetailInfo.ContentFilePath);
Response.AppendHeader("Content-Disposition", "inline; filename=" + contentDetailInfo.ContentFileName);
return File(fileBytes, contentDetailInfo.ContentFileMimeType);
public FileResult Downloads(string file)
{
string diretorio = Server.MapPath("~/Docs");
var ext = ".pdf";
file = file + extensao;
var arquivo = Path.Combine(diretorio, file);
var contentType = "application/pdf";
using (var client = new WebClient())
{
var buffer = client.DownloadData(arquivo);
return File(buffer, contentType);
}
}