如何在 asp.net 中将警报弹出窗口与另存为文件功能结合起来?
我正在尝试编写一个需要保存文件(以 csv 格式)并有一个条件的函数。 是这样的:
如果文件很小,然后 1MB,那么我想在 javascript 中弹出一个警报,然后继续执行另存为文件命令。
我所做的是:
if(...)
{
}
else
{
ClientScript.RegisterStartupScriptBlock(...alert('aaa')...);
}
Response.ContentType = "text/csv";
Response.AppendHeader("Content-Disposition","attachment; filename=file.csv");
Response.TransmitFile("FILE.csv");
Response.End();
另存为工作正常。但在其他情况下不会弹出警报。 我尝试了RegisterStartupScript
。我尝试了很多选择。但似乎 response.end
阻止了客户端脚本的发生。
我正在用 c# asp.net (支持 ajax 的网站,但未使用)、Visual Studio 2005 编写。
有人知道如何绕过这个吗?我怎样才能让它发挥作用?
谢谢你, 加迪姆。
I'm tring to write a function that need to save a file (in csv) and has a condition.
it's something like that:
If the file is small then 1MB then I want to pop up an alert in javascript and then continue with the save as file commands.
What I did was:
if(...)
{
}
else
{
ClientScript.RegisterStartupScriptBlock(...alert('aaa')...);
}
Response.ContentType = "text/csv";
Response.AppendHeader("Content-Disposition","attachment; filename=file.csv");
Response.TransmitFile("FILE.csv");
Response.End();
the save as works fine. but the alert wont popup in the else case.
I tried RegisterStartupScript
. I tried many options. But it seems that the response.end
is stops the clientscript from happening.
I am writing in c# asp.net (ajax enabled website, but not in use), visual studio 2005.
Can any one knows how can I bypass this? How can I make it work?
thank you,
gadym.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您的内容类型是“text/csv”,因此服务器永远不会将您的启动脚本发送到浏览器。即使这样做,浏览器也会将响应作为 csv 文件处理,而不是 html,因此脚本永远不会被执行。
执行此操作的方法是在启动脚本中包含重定向,如下所示:
url 可以指向您现有的页面,并带有一个参数告诉您重新调整 csv,或者您可以编写一个 http 处理程序来返回 csv
The problem is that your content type is "text/csv" so the server will never send your startup script to the browser. Even if it did, the browser will deal with the reponse as a csv file, not as html, and so the script would never be executed.
The way to do this would be to include a redirect in your startup script, something like this:
The url could point to your existing page with a parameter telling you to just retun the csv, or you could write an http handler to return the csv