I have a link that when clicked needs to call a controller action with certain data which must be retrieved via JavaScript. The action will be returning a FileStreamResult.
我有一个链接,当点击时需要调用控制器动作,其中某些数据必须通过JavaScript检索。该操作将返回FileStreamResult。
I looked at @Url.Action but I couldn't figure out how (or even if) I could pass value dictionary stuff which had to be retrieved via JS.
我查看了@ Url.Action,但我无法弄清楚如何(或者甚至)我可以传递必须通过JS检索的值字典内容。
So then I went with a $.post from a click handler. The problem I'm having is that I'm not sure what to do in my success: function() to return the file stream result to the user. Or even if I can.
然后我从点击处理程序中获取了$ .post。我遇到的问题是我不知道该怎么做才能成功:function()将文件流结果返回给用户。或者即使我可以。
So any help on how you would do something like this would be great..
所以对你如何做这样的事情的任何帮助都会很棒..
2 个解决方案
#1
9
So then I went with a $.post from a click handler. The problem I'm having is that I'm not sure what to do in my success: function() to return the file stream result to the user. Or even if I can.
然后我从点击处理程序中获取了$ .post。我遇到的问题是我不知道该怎么做才能成功:function()将文件流结果返回给用户。或者即使我可以。
Exactly. You can't do much with a received byte in javascritpt: obviously you cannot save it on the client computer nor pass it to some external program on the client. So don't call actions that are supposed to return files using AJAX. For those actions you should use normal links:
究竟。你不能对javascritpt中的接收字节做很多事情:显然你不能将它保存在客户端计算机上,也不能将它传递给客户端上的某个外部程序。因此,不要调用应该使用AJAX返回文件的操作。对于这些操作,您应该使用普通链接:
@Html.ActionLink("download file", "download", new { id = 123 })
and let the user decide what to do with the file. You could play with the Content-Disposition
header and set it to either inline
or attachment
depending on whether you want the file to be opened with the default associated program inside the browser or prompt the user with a Save File dialog.
并让用户决定如何处理该文件。您可以使用Content-Disposition标头并将其设置为内联或附件,具体取决于您是希望使用浏览器内的默认关联程序打开文件,还是使用“保存文件”对话框提示用户。
UPDATE:
It seems that I have misunderstood the question. If you want to append parameters to an existing link you could subscribe for the click event in javascript and modify the href by appending the necessary parameters to the query string:
我似乎误解了这个问题。如果要将参数附加到现有链接,可以在javascript中订阅click事件,并通过将必要参数附加到查询字符串来修改href:
$(function() {
$('#mylink').click(function() {
var someValue = 'value of parameter';
$(this).attr('href', this.href + '?paramName=' + encodeURIComponent(someValue));
return true;
});
});
#2
0
Instead of going with a post, I'd go with associate a JQuery on click handler of the link which would call the controller action. This is assuming that the action method returns a FileStreamResult and sets the correct content type so that the browser interprets the result and renders it accordingly.
我不会使用帖子,而是在链接的点击处理程序上使用JQuery来调用控制器操作。这假设action方法返回FileStreamResult并设置正确的内容类型,以便浏览器解释结果并相应地呈现它。
With your approach you'd have to interpret in the onSuccessHandler of the post on how to render the generated stream.
使用您的方法,您必须在帖子的onSuccessHandler中解释如何呈现生成的流。
#1
9
So then I went with a $.post from a click handler. The problem I'm having is that I'm not sure what to do in my success: function() to return the file stream result to the user. Or even if I can.
然后我从点击处理程序中获取了$ .post。我遇到的问题是我不知道该怎么做才能成功:function()将文件流结果返回给用户。或者即使我可以。
Exactly. You can't do much with a received byte in javascritpt: obviously you cannot save it on the client computer nor pass it to some external program on the client. So don't call actions that are supposed to return files using AJAX. For those actions you should use normal links:
究竟。你不能对javascritpt中的接收字节做很多事情:显然你不能将它保存在客户端计算机上,也不能将它传递给客户端上的某个外部程序。因此,不要调用应该使用AJAX返回文件的操作。对于这些操作,您应该使用普通链接:
@Html.ActionLink("download file", "download", new { id = 123 })
and let the user decide what to do with the file. You could play with the Content-Disposition
header and set it to either inline
or attachment
depending on whether you want the file to be opened with the default associated program inside the browser or prompt the user with a Save File dialog.
并让用户决定如何处理该文件。您可以使用Content-Disposition标头并将其设置为内联或附件,具体取决于您是希望使用浏览器内的默认关联程序打开文件,还是使用“保存文件”对话框提示用户。
UPDATE:
It seems that I have misunderstood the question. If you want to append parameters to an existing link you could subscribe for the click event in javascript and modify the href by appending the necessary parameters to the query string:
我似乎误解了这个问题。如果要将参数附加到现有链接,可以在javascript中订阅click事件,并通过将必要参数附加到查询字符串来修改href:
$(function() {
$('#mylink').click(function() {
var someValue = 'value of parameter';
$(this).attr('href', this.href + '?paramName=' + encodeURIComponent(someValue));
return true;
});
});
#2
0
Instead of going with a post, I'd go with associate a JQuery on click handler of the link which would call the controller action. This is assuming that the action method returns a FileStreamResult and sets the correct content type so that the browser interprets the result and renders it accordingly.
我不会使用帖子,而是在链接的点击处理程序上使用JQuery来调用控制器操作。这假设action方法返回FileStreamResult并设置正确的内容类型,以便浏览器解释结果并相应地呈现它。
With your approach you'd have to interpret in the onSuccessHandler of the post on how to render the generated stream.
使用您的方法,您必须在帖子的onSuccessHandler中解释如何呈现生成的流。