生成的文件dowloaded后重定向/显示视图 [英] Redirect / show view after generated file is dowloaded
问题描述
我有一个下载一个动态生成文件的控制器操作:
I've got a controller action that downloads a dynamically generated file:
public ActionResult DownloadFile()
{
var obj = new MyClass { MyString = "Hello", MyBool = true };
var ser = new XmlSerializer(typeof(MyClass));
var stream = new MemoryStream();
ser.Serialize(stream, obj);
stream.Position = 0;
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.xml");
Response.ContentType = "application/xml";
// Write all my data
stream.WriteTo(Response.OutputStream);
Response.End();
return Content("Downloaded");
}
仅供参考:
public class MyClass
{
public string MyString { get; set; }
public int MyInt { get; set; }
}
这是工作,并且该文件(myfile.xml中)被下载。结果
然而,已下载的消息不发送给浏览器。
This is working, and the file (myfile.xml) is downloaded.
However, the message "Downloaded" is not sent to the browser.
同样,如果我取代返回内容(已下载);
结果
为返回重定向(www.something.com);
结果
那么浏览器在文件下载之前重定向。
Similarly, if I replace return Content("Downloaded");
for return Redirect("www.something.com");
then the browser is redirected before the file downloads.
作为一个有点pre-缓行,用户旅程是:
As a bit of a pre-amble, the user journey is:
- 用户填写的previous视图形式
- 表单提交
- 生成XML和下载
- 用户被重定向/已下载视图显示的(所以按F5不会重新发布该表格)的
- User fills out form on previous view
- Form is submitted
- XML is generated and downloaded
- User is redirected / "Downloaded" view is shown (so hitting F5 won't re-post the form)
推荐答案
每个HTTP请求只能有一个反应 - 你想在两到潜行(文件和网页)
Each HTTP request can only have one response - you're trying to sneak in two (the file, and a page).
通常,当你发送一个内容处置:附件。HTTP头,浏览器将停留在当前页面上弹出一个文件保存对话框(或自动保存在您的下载文件)
Normally when you send a "Content-Disposition: attachment" HTTP header the browser will stay on the current page and pop a file save dialog (or automatically save the file in your downloads).
您将不得不如果你想prevent重新提交表单来改变你的策略。我建议有点JavaScript来禁用该表单的提交按钮,显示一个div覆盖已完成的消息?
You're going to have to change your strategy if you want to prevent re-submission of the form. I'd suggest a bit of javascript to disable the form's submit button and show the "Completed" message in a div overlay?
这篇关于生成的文件dowloaded后重定向/显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!