Fru*_*ler 2 model-view-controller asp.net-mvc
提交表单(form.submit()且没有ajax)时,有没有办法检测响应是否回来(假设没有加载新页面).从控制器我实际上返回的是一个文件,而不是一个新的View.
视图:
<% using (Html.BeginForm()){%>
....
<input id="submitsearch" type="submit" value="DownloadFile" name="SubmitButton" />
<%} %>
Run Code Online (Sandbox Code Playgroud)
控制器:
return File(FileContent, "text/plain", Filename);
Run Code Online (Sandbox Code Playgroud)
基本上我想要发生的是,当用户点击提交时,我会显示一个加载图标,当出现下载弹出窗口时,我想删除加载图标.
所以我实际上并不需要阅读响应,但只知道响应何时返回,以便我可以删除加载图标.
限制是我不能使用ajax调用来提交页面.
干杯.
您可以使用我称之为cookie轮询的技术:
<% using (Html.BeginForm("Download", "Home")) { %>
<%= Html.Hidden("downloadToken", DateTime.Now.Ticks) %>
<input type="submit" value="Download" />
<% } %>
<script src="<%= Url.Content("~/Scripts/jquery.cookie.js") %>" type="text/javascript"></script>
<script type="text/javascript">
$('form').submit(function () {
// We start a download => show some progress indicator here
$(':submit', this).val('Please wait while downloading...').attr('disabled', 'disabled');
// start polling for the cookie every 500ms
var fileDownloadCheckTimer = window.setInterval(function () {
var cookieValue = $.cookie('fileDownloadToken');
var token = $('#downloadToken').val();
if (cookieValue == token) {
// The download has finished => remove the progress indicator
$(':submit', $('form')).val('Download').removeAttr('disabled');
// remove the cookie
$.cookie('fileDownloadToken', null);
// stop polling
window.clearInterval(fileDownloadCheckTimer);
}
}, 500);
});
</script>
Run Code Online (Sandbox Code Playgroud)
并在控制器动作内:
public ActionResult Download(string downloadToken)
{
// Simulate a slow download
Thread.Sleep(5000);
var cookie = new HttpCookie("fileDownloadToken", downloadToken);
// set the cookie with the proper value
Response.AppendCookie(cookie);
return File(Encoding.UTF8.GetBytes("foo bar"), "text/plain", "foo.txt");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1850 次 |
| 最近记录: |