返回文件时,使用MVC3 AJAX调用ActionResults失败
我有一个操作结果,它返回一个创建的文件对象,该对象经过简化后可显示!:返回文件时,使用MVC3 AJAX调用ActionResults失败,ajax,asp.net-mvc-3,Ajax,Asp.net Mvc 3,我有一个操作结果,它返回一个创建的文件对象,该对象经过简化后可显示!: public ActionResult Create(int recID, int templateID) { //Get dbRecord from recID DBRecord dbRecord = db.dbRecord.Find(recID); //Get Template from templateID Template template = db.Templates.Find(t
public ActionResult Create(int recID, int templateID)
{
//Get dbRecord from recID
DBRecord dbRecord = db.dbRecord.Find(recID);
//Get Template from templateID
Template template = db.Templates.Find(templateID);
if (template == null) throw new FileLoadException(string.Format("No database record found for template reference {0}",templateID));
//set fileOutput details
WordFile fileOutput = new WordFile(dbRecord, Server.MapPath("~/Documents/"),template);
//Create XML object for Template
XmlDocument xDoc = new XmlDocument();
//Save resulting document
xDoc.InnerXml = mergeData(template,dbRecord);
xDoc.Save(fileOutput.fullName);
if (!System.IO.File.Exists(fileOutput.fullName)) throw new FileNotFoundException(string.Format("File {0} could not be created", fileOutput.fileName));
//Return saved document
return File(fileOutput.fullName, "application/doc", fileOutput.fileName);
}
如果我通过HTML路由调用它
<%: Html.RouteLink("Generate via HTML
, "GenerateDocument"
, new RouteValueDictionary(
new {
controller = "Template"
, action = "Create"
, recID = 1
,templateID = 1
}
)
)%>
编辑1:它工作正常,生成文档并提示用户打开、保存等。但是,如果我通过AJAX RouteLink调用它,代码将逐步执行,文档将在服务器上创建,但不会提示用户打开或保存
<%: Ajax.RouteLink("Generate via AJAX"
, "GenerateDocument"
, new RouteValueDictionary(
new {
controller = "Template"
, action = "Create"
, recID = 1
, templateID = 1 }
)
, new AjaxOptions
{
HttpMethod="POST"
, LoadingElementId="Refresh"
})%>
是否存在阻止AJAX返回文件类型的固有限制,或者我是否错过了找到明显答案的正确搜索组合?我不确定我是否理解您所说的内容。是否尝试将文件内容返回到javascript变量?这是我唯一能想到的AJAX RouteLink的好处。如果希望它提示用户打开或保存,请使用普通链接。如果您试图在页面中嵌入文件,请不要返回文件,而是返回用于嵌入的html。如果我通过AJAX RouteLink调用它,它会失败,这到底是什么意思?怎么会失败?是的,对不起,这有点模糊。。。它点击ActionResult,逐步执行代码,不返回生成的文档。当Html版本结束时,它会提示用户打开或保存文档*用户单击“生成文档”链接*调用操作结果。在执行代码时,对于大型文档来说,这可能是一个漫长的过程,用户有一个进度指示器*生成文档并提示用户打开/保存/等。用户以不耐烦著称,我想在步骤2使用AJAX.LoadingElementId通知他们。如果AJAX调用只能返回文本值而不是文件结果,那么是否可以使用onSuccess函数调用一个单独的ActionResult来传递所创建文档的url?