大家好,想知道是否有人可以提供帮助;我已经编写了这段代码,它将生成一个 excel 电子表格并将其保存到指定位置。然后我想通过从存储位置读取文件然后询问用户他们想要存储它的位置来显示“另存为”对话框。 excel文件生成正常,我可以正常打开它!但是我的问题是我编写的代码似乎是将文件直接输出到我的浏览器,所以我在浏览器屏幕上得到了 excel 文件的所有内容,而不是像预期的那样显示另存为对话框!
public ActionResult FormSuccess()
{
String FileName = System.Configuration.ConfigurationManager.AppSettings["FileName"].ToString();
String FilePath = System.Configuration.ConfigurationManager.AppSettings["FileSaveLocation"].ToString();
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/vnd.xls";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath + FileName);
response.End();
return PartialView("FormSuccess");
}
最佳答案
哟文斯,技巧怎么样?还戴着奖章吗? :)
您不应该使用 FileContentResult 而不是 PartialView 吗?您将无法在同一调用中返回文件和 HTML“成功”内容 - 您可能应该先调用 PartialView,然后它会使用 javascript 在新窗口中打开 FileContentResult URL。
看这个:http://www.mikesdotnetting.com/Article/125/ASP.NET-MVC-Uploading-and-Downloading-Files
还有这个网址:
http://weblogs.asp.net/rajbk/archive/2010/05/03/actionresult-types-in-mvc2.aspx
关于c# - MVC C# 下载文件并另存为对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6704745/