Problema con window.open en FireFox

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.

Codigo del cliente:

 <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>

Lado del servidor:

[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:

Error de servidor en la aplicación '/'.

Error HTTP 400: solicitud incorrecta.

and in url I can see for example: http://localhost:1406/\Files\test.zip

How can I return the correct path to zip file?

preguntado el 16 de mayo de 11 a las 20:05

Is this file located on your internal network, on at an internet-accessible location? -

you might just need to make your "\\" be "/" -

Have you tried using forward slash characters instead of backslashes? You're forming a URL, after all, and backslashes are not valid separators in URLs. -

Avitus, Pointy, your answers is good for me. Thanks. -

4 Respuestas

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

contestado el 17 de mayo de 11 a las 00:05

Change the server side code to:

[System.Web.Services.WebMethod]
public static string Test()
{
    return "/Files/test.zip";
}

Web URLs should use forward-slashes instead of backslashes.

contestado el 17 de mayo de 11 a las 00:05

En general deberías usar ResolveUrl method of the System.Web.UI.Control class. But in case of static method there is some workaround solutions.

contestado el 24 de mayo de 11 a las 17:05

Replacing the following lines

[System.Web.Services.WebMethod]
public static string Test()
{
  return "\\Files\\test.zip";
}

with these might work...

[System.Web.Services.WebMethod]
public static string Test()
{
  return "\\\\Files\\test.zip";
}

contestado el 17 de mayo de 11 a las 00:05

The downvote wasn't mine, but perhaps it was because the string is already escaped using C# conventions. - Katie Kilian

Well I didn't downvote but your answer definitely won't help - you've added más backslashes, but what needs to be done is to change to "/" characters instead. - Puntiagudo

adding more backslashes would render it the same way. the four backslashes would be considered as two escape sequences and the path he would get eventually is: \\Files\Test.zip, which was what he was looking for... - Trasero naveed

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.