gpt4 book ai didi

javascript - 下载 asp.net 后触发 javascript 函数

转载 作者:行者123 更新时间:2023-12-03 07:36:19 25 4
gpt4 key购买 nike

我在代码后面有一个方法,可以将文件从服务器下载到客户端。之后我想运行一个 javascript 函数。但这不起作用。仅当 javascript 函数位于按钮单击事件中时才有效。

string imageFilePath = Server.MapPath("~/TireLabel.png");
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file

Graphics g = Graphics.FromImage(bitmap);
string text = "215/45 R 20";
g.DrawString(text, drawFontArial26Bold, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);

text = "013";
g.DrawString(text, drawFontArial20Regular, drawBrush, new RectangleF(70, 45, 0, 0), drawFormatRight);

text = "1";
g.DrawString(text, drawFontArial20Regular, drawBrush, new RectangleF(100F, 80, 0, 0), drawFormatRight);

text = "2";
g.DrawString(text, drawFontArial20Regular, drawBrush, new RectangleF(240, 80, 0, 0), drawFormatRight);

Bitmap bit = new Bitmap(bitmap);
bit.Save(Server.MapPath("~/TireLabel.bmp"), System.Drawing.Imaging.ImageFormat.Bmp);

Response.ContentType = "application/jpeg";
Response.AddHeader("content-disposition", "attachment; filename=" + Server.MapPath("~/TireLabel") + ".bmp");
Response.WriteFile(Server.MapPath("~/TireLabel.bmp" + ""));

ClientScript.RegisterStartupScript(GetType(), "key", "runPrint();", true); // THIS does not fire.

//Javascript function
function runPrint() {
var objShell = new ActiveXObject("WScript.Shell");
var strCommand = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
//var strCommand = "C:\\PrintLabel.exe";
objShell.Exec(strCommand);
//objShell.ShellExecute(strCommand, "","","open","1");
}

如何让 JavaScript 火起来。

最佳答案

这是一个难题,因为浏览器通常不会告诉您下载何时完成。看看jQuery.filedownload允许您通过 AJAX 下载文件。

-- 编辑--

以下代码将 jQuery filedownload 附加到具有“fileDownload”类的所有链接:

 $(document).on('click', 'a.fileDownload', function() {        

$.fileDownload($(this).prop('href'))
.done(function () { alert('File download a success!'); })
.fail(function () { alert('File download failed!'); });

return false; //this is critical to stop the click event which will trigger a normal file download
});

这假设 Controller 操作的 URL 在链接的 href 属性中给出,并且此操作通过 return File() 返回一个文件。响应还必须包含 cookie /fileDownload 以通知 jquery.fileDownload 文件下载已成功。

//jquery.fileDownload uses this cookie to determine that a file download has completed successfully
response.AppendCookie(new HttpCookie("fileDownload", "true") {
Path = "/"
});

-- 编辑 2 - 更改了源代码以显示依赖性更少的更简单的示例。使用 .done promise 在下载完成后触发操作

关于javascript - 下载 asp.net 后触发 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35599515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com