我想显示加载程序,直到文件下载开始 我该怎么办?
这是我的代码:
$('body').prepend("<form method='post' action='create' id='doneform'>" +
"<input type='hidden' name='data' value='" + data + "' >" +
"<input type='hidden' name='brandcolor' value='" + brandcolor + "' >" +
"</form>");
$('#doneform').submit();
controller.cs
[HttpPost]
public ActionResult create()
{
...some stuff....
System.Web.HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition",
String.Format("{0}; filename={1}.pdf;size={2}", "attachment", filename, pdfBuffer.Length));
System.Web.HttpContext.Current.Response.BinaryWrite(pdfBuffer);
System.Web.HttpContext.Current.Response.End();
}
答案 0 :(得分:0)
http://syper-blogger.blogspot.in/2011/08/how-to-detect-start-of-file-download.html
这解决了问题.............
答案 1 :(得分:0)
我不认为这是一种正确的做法,但你可以试试这个
<img id="loading-image" src="loader.gif" style="display:none" />
$("#doneform").submit(function(event) {
$('#loading-image').show();
event.preventDefault();
var data = $(this).find('input[name="data"]').val();
var brandcolor = $(this).find('input[name="brandcolor"]').val();
$.ajax({
url: '/[Controller]/[Action]',
cache: false,
data:{ data: data, brandcolor:brandcolor },
success: function(html){
// Do your stuff.
},
complete: function(){
$('#loading-image').hide();
},
error: function () {
// Handle any errors here.
}
});
});
您可以尝试 FileResult
public FileResult create()
{
var fileName = "Something.pdf";
//Create your pdf file here and save it into a location
byte[] fileBytes = System.IO.File.ReadAllBytes("Path for the created pdf file");
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}