我的 Asp.NET HttpGet 操作(返回 FileResult)仅在 jquery post 之后在对象标记的 src 中调用一次
我有一个由 javascript 动态加载的标签对象。该标签是在 jquery post 之后加载的:
$.post('@Url.Action("ShowCredential", "ManageCredentials")',
$(form).serialize(), function(url) {
document.getElementById("credential_preview").innerHTML = "<object id='credencial_atual' type='application/pdf' classid='clsid:CA8A9780-280D-11CF-A24D-444553540000' width='250' height='420' style='border: 1px solid'> <param name='src' value='" + url + "#navpanes=0&scrollbar=0&zoom=100%' /></object>";
$("#preview_popup").show();
});
Obs: i load the form variable with my form.
在“ShowCredential”操作的代码隐藏中,我在 byte[] 中加载 pdf 并存储在我的用户会话中:
[HttpPost]
public string ShowCredential(/* the attributes to help to load the pdf */)
{
// Loading my pdf...
Session.User.CurrentPDF = // set the pdf loaded
UrlHelper urlHelper = new UrlHelper(this.ControllerContext.RequestContext);
string url = urlHelper.Action("GetPDF", "ManageCredentials");
return url;
}
url 是使用将返回 pdf 的操作生成的。
[HttpGet]
public FileResult GetPDF()
{
return File(Session.User.CurrentPDF, "application/pdf");
}
所以,第一次,好吧,加载了正确的pdf,但在第二次、第三次……加载了相同的pdf,为什么? (我检查了我是否传递了正确的参数,是的,我通过了=))
观察:当我发布数据来加载pdf时,在jquery返回之后,我的代码第一次调用操作GetPDF,但是,当我发布时再次,不再调用 GetPDF 操作。
I have a tag object that is loaded dinamically by javascript. This tag is loaded after a jquery post:
$.post('@Url.Action("ShowCredential", "ManageCredentials")',
$(form).serialize(), function(url) {
document.getElementById("credential_preview").innerHTML = "<object id='credencial_atual' type='application/pdf' classid='clsid:CA8A9780-280D-11CF-A24D-444553540000' width='250' height='420' style='border: 1px solid'> <param name='src' value='" + url + "#navpanes=0&scrollbar=0&zoom=100%' /></object>";
$("#preview_popup").show();
});
Obs: i load the form variable with my form.
In my code-behind of the action "ShowCredential" i load a pdf in byte[] and store in my user session:
[HttpPost]
public string ShowCredential(/* the attributes to help to load the pdf */)
{
// Loading my pdf...
Session.User.CurrentPDF = // set the pdf loaded
UrlHelper urlHelper = new UrlHelper(this.ControllerContext.RequestContext);
string url = urlHelper.Action("GetPDF", "ManageCredentials");
return url;
}
The url is generated with the action that will return the pdf.
[HttpGet]
public FileResult GetPDF()
{
return File(Session.User.CurrentPDF, "application/pdf");
}
So, in the first time, ok, is loaded the right pdf, but in the second, third... is loaded the same pdf, why? (i checked if i pass the right params, yes i pass =))
Obs: When i post the data to load the pdf, after - in jquery return - my code call the action GetPDF in the first time, but, when i post again, the action GetPDF is not called anymore.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码看起来很奇怪。您正在向某些控制器操作发送 POST AJAX 请求,但您似乎没有在成功回调中对结果(
dados
变量)执行任何操作。此外,您似乎调用了 VisualizarCredencial 操作两次:一次用于 AJAX 请求,一次用于渲染 PDF。你还没有解释你的意图,所以我只能猜测你想做什么,我的猜测是你可能有2个控制器操作:一次返回可用于查询第二个控制器操作的url或id这将返回pdf。
像这样:
现在在客户端上您可以使用 iframe:
并且为了避免
GepPdf
操作潜在的缓存问题,您可以使用 自定义[NoCache]
操作过滤器。Your code seems weird. You are sending a POST AJAX request to some controller action but you don't seem to be doing anything in the success callback with the result (
dados
variable). Also you seem to be calling theVisualizarCredencial
action twice: once for the AJAX request and once for rendering the PDF.You haven't explained your intentions so I can only be guessing what you are trying to do, and my guess is that you could have 2 controller actions: once that returns the url or an id that could be used to query the second controller action that will return the pdf.
Like this:
and now on the client you could use an iframe:
and to avoid potential caching problems with the
GepPdf
action you could decorate it with a custom[NoCache]
action filter.维尼修斯,
希望以下内容对您有所帮助。在我的一个应用程序中,我必须显示 Word 文档、PDF、图像或任何其他类型的文档。我知道这本身并不是通过 ajax 请求的,但可能会让您想到替代解决方案。以下代码实现了这一点(忽略对象模型,而是特别检查 switch 语句):
当然,结果不会显示在视图内,因为需要适当的“应用程序”来显示每个结果,因此 Adobe PDFReader 将是在你的情况下打开。
Vinicius,
Hopefully the following will help you. In one of my apps I have to display either a word doc, a pdf, an image or any other type of doc. I appreciate that this isn't requested via ajax per se, but may allow you to think of an alternative solution. The following code achieves this (ignore the object model and instead examine the switch statement in particular):
Of course, the result isn't displayed inside the view, as the appropriate 'app' is required to display each, thus Adobe PDFReader would be opened in your case.
寻找答案,我得出以下结论:所以,如果我们使用 form.submit() 那么控制器只返回一个文件结果。如果我们使用 ajax 传递数据,我们无法返回 pdf,但必须返回您在 get 上传递的内容(例如 href),它将返回 pdf。
Looking for the answer, i reach the following conclusion: so, if we use a form.submit() then the controller just return a file result. If we use ajax to pass the data, we cannot return the pdf, but must return something you pass on a get (for exemple href) that will return the pdf.