守候你守候我
2022-05-22 19:21:30
我正在提交一個向后端發送請求的表單,C# 操作。后端生成一個pdf文件并發回瀏覽器下載。我如何捕捉這個事件?我只是想在從服務器發送 pdf 時向用戶顯示一條消息。我不能使用 ajax,因為我正在通過表單發送請求,并且我希望響應是附件。 public ActionResult ConvertHTMLtoPDF(string htmltoPDfFullUrl) { Byte[] res = null; using (MemoryStream ms = new MemoryStream()) { // not important logic } var stream = new MemoryStream(res); return new FileStreamResult(stream, "application/pdf") { FileDownloadName = "some name.pdf" }; }$(document).on('click', ".formSubmitDiv", function () { formSubmit();})function formSubmit() { $('#htmltoPDfFullUrl').val(fullHTMLLIVE); document.getElementById('beginConvertHTMLtoPDF').submit();}@using (Html.BeginForm("ConvertHTMLtoPDF", "Home", FormMethod.Post, new { id = "beginConvertHTMLtoPDF" })){ <input type="hidden" name="htmltoPDfFullUrl" id="htmltoPDfFullUrl" />}
1 回答

BIG陽
TA貢獻1859條經驗 獲得超6個贊
我設法通過從服務器發送一個 cookie 并在客戶端上不斷檢查這個 cookie 來解決這個問題。
這就是我發送cookie的方式。
Response.Cookies.Add(new HttpCookie(serverCookieResponse + "_CookiePDFResponse", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ff")));
這就是我檢查cookie是否已收到的方式
function checkCookie() {
let cookieName = $('#serverCookieResponse').val() + "_CookiePDFResponse";
var cookieVal = $.cookie(cookieName);
if (cookieVal == null || cookieVal === 'undefined') {
setTimeout("checkCookie();", 1000);
}
else {
document.cookie = cookieName + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
}
希望這對某人有幫助!
添加回答
舉報
0/150
提交
取消