C# 如何显示打开/保存对话框asp net mvc 4
我可以要求一个文件,也有它返回。 我不知道如何显示打开/保存对话框 查看:C# 如何显示打开/保存对话框asp net mvc 4,c#,javascript,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Javascript,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我可以要求一个文件,也有它返回。 我不知道如何显示打开/保存对话框 查看: function saveDocument() { $.ajax({ url: '/Operacao/saveDocument', type: 'POST', DataType: "html", success: function (data) { //I get the file content here }
function saveDocument() {
$.ajax({
url: '/Operacao/saveDocument',
type: 'POST',
DataType: "html",
success: function (data) {
//I get the file content here
}
});
}
public void saveDocument() {
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename=SailBig.jpg");
Response.TransmitFile(Server.MapPath("~/MyPDFs/Pdf1.pdf"));
Response.End();
}
控制器:
function saveDocument() {
$.ajax({
url: '/Operacao/saveDocument',
type: 'POST',
DataType: "html",
success: function (data) {
//I get the file content here
}
});
}
public void saveDocument() {
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename=SailBig.jpg");
Response.TransmitFile(Server.MapPath("~/MyPDFs/Pdf1.pdf"));
Response.End();
}
我认为您无法在浏览器异步下载文件,只需将用户重定向到操作,浏览器将打开一个保存对话框窗口。在asp.net mvc中,您可以使用基本控制器的
file
方法下载文件,从而生成FileResult
public ActionResult SaveDocument()
{
string filePath = Server.MapPath("~/MyPDFs/Pdf1.pdf");
string contentType = "application/pdf";
//Parameters to file are
//1. The File Path on the File Server
//2. The content type MIME type
//3. The parameter for the file save by the browser
return File(filePath, contentType, "Report.pdf");
}
强制firefox(不适用于chrome)打开保存对话框的一种方法是将contenttype设置为“application/octet stream”,并为其提供具有正确扩展名的文件名
public ActionResult SaveDocument()
{
string filePath = Server.MapPath("~/MyPDFs/Pdf1.pdf");
string contentType = "application/octet-stream"; //<---- This is the magic
//Parameters to file are
//1. The File Path on the File Server
//2. The content type MIME type
//3. The parameter for the file save by the browser
return File(filePath, contentType, "Report.pdf");
}
public ActionResult SaveDocument()
{
字符串filePath=Server.MapPath(“~/MyPDFs/Pdf1.pdf”);
string contentType=“应用程序/八位字节流”;//它正在自动下载,无需询问。对话框不显示!这取决于浏览器。如果设置为自动下载到给定文件夹,浏览器将自动下载。Firefox和Chrome是具有这种行为的浏览器。感谢您的关注!@JoãoSimões是否有办法获取另存为对话框
是否显示在Firefox和Chrome中?我正在搜索解决方案!对于Chrome:在“设置”中,单击“显示高级设置”并向下滚动到“下载”部分。要更改默认下载位置,请单击“更改”并选择要保存文件的位置。如果您希望为每次下载选择特定位置,请选择“下载前询问每个文件的保存位置”复选框。