FireFox 中 window.open 的问题
我在 Firefox 中下载文件时遇到问题。我试图在旧帖子中找到解决方案,但没有找到任何东西。我知道解决方案非常简单,但我认为今天不是我的幸运日:)
简单的例子。我尝试从 JavaScript 调用 Web 方法并下载文件。
客户端代码:
<script language="javascript" type="text/javascript">
function Test() {
PageMethods.Test(onCompleted);
}
function onCompleted(result) {
window.open(result);
}
</script>
........
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div>
<input type=button value="Download" onclick="Test()"/>
</div>
服务器端:
[System.Web.Services.WebMethod]
public static string Test()
{
return "\\Files\\test.zip";
}
文件夹“Files”位于 Web 应用程序的根文件夹中。
对于 IE 和 Chrome,此代码工作正常,我可以下载该文件。但在 Firefox 中,我收到错误:
“/”应用程序中的服务器错误。
HTTP 错误 400 - 错误请求。
在 url 中我可以看到例如: http://localhost:1406/\Files\test.zip
如何返回 zip 文件的正确路径?
I have problem with downloading files in Firefox. I tried to find solution in old posts but I didn't find anything. I understand that solution is very simple, but I think today is not my lucky day :)
Simple example. I try to call a web method from JavaScript and download a file.
Client code:
<script language="javascript" type="text/javascript">
function Test() {
PageMethods.Test(onCompleted);
}
function onCompleted(result) {
window.open(result);
}
</script>
........
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div>
<input type=button value="Download" onclick="Test()"/>
</div>
Server side:
[System.Web.Services.WebMethod]
public static string Test()
{
return "\\Files\\test.zip";
}
Folder 'Files' lies in root folder of Web application.
For IE and Chrome, this code is working fine, and I can download the file. But in Firefox, I get an error:
Server Error in '/' Application.
HTTP Error 400 - Bad Request.
and in url I can see for example:
http://localhost:1406/\Files\test.zip
How can I return the correct path to zip file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
URL 不允许使用反斜杠。
如果该文件位于 Windows Web 服务器根目录上的 \Files\test.zip,则该文件的正确 URL 为 http:///Files/test.zip
URL don´t allow for backslashes.
If the file is located at \Files\test.zip on your windows webserver root the correct url to the file is http:///Files/test.zip
将服务器端代码更改为:
Web URL 应使用正斜杠而不是反斜杠。
Change the server side code to:
Web URLs should use forward-slashes instead of backslashes.
一般来说,您应该使用 ResolveUrl 方法System.Web.UI.Control 类的。但对于静态方法,有一些 解决方案。
In general you should use ResolveUrl method of the System.Web.UI.Control class. But in case of static method there is some workaround solutions.
将以下行替换
为这些行可能会起作用......
Replacing the following lines
with these might work...