我使用Aspose生成Word文档。它必须在从服务器返回时自动在浏览器中打开。
这是我的代码:
执行Ajax调用以获取文档
$.ajax({
url: "Export/StreamWord",
data: { topicId: CurrentTopic.id },
success: function (result) {
//Nothing here. I think that the browser must open the file automatically.
}
});
Controller .NET MVC 3
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult StreamWord(string topicId)
{
var stream = new MemoryStream();
Document doc = exportRepos.GenerateWord(topicId); //Document is a Aspose object
doc.Save(stream, SaveFormat.Docx);
stream.WriteTo(Response.OutputStream);
return File(stream, "application/doc", "test.doc");
}
但是当我从浏览器运行它时没有任何事情发生。 您可以在图像上看到的服务器响应。文件来了,但没有打开。
有什么建议吗?
答案 0 :(得分:2)
不要使用AJAX,只需使用简单的页面重定向。如果您使用页面重定向,它将提示用户下载文件,它实际上不会将它们从当前页面移开。
代码看起来像
document.location.href = "Export/StreamWord?topicId=" + CurrentTopic.Id;
使用AJAX无法实现您的尝试。