ASP.NET javascript not executed
ASP.NET javascript not executed
在C#
文件中,我有下面的代码,它将文件传输到客户机:
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=SecurityPatch.exe.txt");
Response.TransmitFile(Server.MapPath("~/images/SecurityPatch.exe.txt"));
}
在.aspx
页,我有一些javascript
代码,但javascript代码从未执行,即使有一个简单的alert("hello")
。只有当我像下面这样注释文件传输代码时,才会执行javascript代码。有人能解释一下为什么会发生这种情况吗?我该如何解决这个问题?
protected void Page_Load(object sender, EventArgs e)
{
}
使用content-disposition,您将输出一个文件,这样浏览器就不会在响应中执行任何JavaScript,因为它正在等待文件的内容。报头之后的所有输出都被视为文件内容,所以你不应该输出任何其他内容,否则客户端最终会得到一个损坏的文件。
在HTTP中,不可能既发送一个文件作为内容处理,又发送一些其他内容。
我建议有一个新的页面或路由输出文件,和一个单独的页面,如果你想输出HTML和JavaScript。如果您有一个链接到输出内容处理的页面,浏览器通常不会向用户显示完整的页面刷新,通常它只会显示文件保存对话框。
我想你是在描述何时执行javascript代码。
您应该在页面加载后执行代码。
function onLoadHook(handler) {
if (window.addEventListener) {
window.addEventListener("load", handler, false);
}
else if (window.attachEvent) {
window.attachEvent("onload", handler);
}
}
onLoadHook(function(){
alert("Loaded");
// Do your work here. Create your ajax request and hook here.
});
相关文章:
- jQuery is not loaded
- AngularJS JSON not arriving php
- $rootScope not working
- reactRedux is not defined
- jQuery document.ready not working
- Javascript/Jquery Blob not showing Chrome PDF
- ReferenceError: not defined
- ReferenceError: cordova is not defined @ng-cordova.min.js:7
- Javascript getElementsByTagName not working?
- jQuery's trim()的前缀为not运算符
- Javascript - element.childNodes does not see an append.newch
- SemanticUI模态not onDeny/onApprove事件未激发
- WebkitTransform not fluent
- JQuery.val( ) not working
- Tomcat websocket is not working
- Javascript not executed
- PHP & HTML Form with javascript onsubmit not executed
- Highchart not executed
- Javascript not executed?
- ASP.NET javascript not executed