Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 914
  • Last Modified:

How do you escape ampersand in IE File Download dialog box?

How do you escape ampersand in IE's "file download dialog box"?

I'm trying to download a file that has an ampersand on its file name (eg. "Batman & Robin.xls" )

IE File Download converts ampersand(&) to an underscore(_) (eg. "Batman _Robin.xls")

I tried escaping it with &&, but the file when downloaded also produces a double ampersand(&&)
0
niko_edralin
Asked:
niko_edralin
  • 4
  • 3
  • 3
2 Solutions
 
fcardinauxCommented:
Have you tried to use the Javascript encodeURIComponent() function http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp ?
0
 
fcardinauxCommented:
Go to http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_encodeURIComponent, type:

document.write(encodeURIComponent("Batman & Robin.xls"));

and you will get:

Batman%20%26%20Robin.xls
0
 
Louis01Commented:
Replace the & with %26
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
fcardinauxCommented:
Louis01: if you had read my comments, you would have seen that my solution does exactly this. Moreover, it encodes all special characters, not just &.
0
 
Louis01Commented:
fcardinaux: You are 100% correct. I usually open about 5 questions in different tabs. (This was the last one I got to.) My view on the Q was before you proposed your answer. I did not refresh before I posted my answer.
0
 
niko_edralinAuthor Commented:
I did both of your solutions. And the "file download dialog box" still <b>displays</b> "Batman _Robin.xls"

&& works great, but the downloaded file also has 2 ampersands "Batman && Robin.xls"
0
 
niko_edralinAuthor Commented:
This is the Java code that I'm using, hope it helps.


    private String getFileName() {
 
        String str=prefix+=" ("+_workbookSettings.getCode()+"-"+_workbookSettings.getDescription()+")";
 
        str=str.replaceAll(" ", "%20");//issue for i.e.7
        str=str.replaceAll("\\.", "%2e");
        str=str.replaceAll("&", "%26");
        return str+".xls";
    }
 
  public void finished(){
       ...
       ...
       ...
       resp.setContentType("application/vnd.ms-excel");
       resp.addHeader("content-disposition",
                                   "inline;    
       filename=encodeURIComponent(" + getFileName() + ")");
 
       workbook.write(out);
 
       out.flush();
       out.close();
 
       ...
       ...
    }

Open in new window

0
 
Louis01Commented:
Try replacing the line
   str=str.replaceAll("&", "%26");
with
   str = str.replaceAll(/&/,"%26");
OR
   str = str.replaceAll(/&amp;/,"%26");
OR
   str = str.replaceAll(/\x26/,"%26");
----------------------
Alternatively, you could try removing the 3 str=str.replaceAll lines in getFileName. (If that works, the solution proposed by fcardinaux is the correct one.)
0
 
niko_edralinAuthor Commented:
Still doesn't work :(

anyway, I was able to force our QA to accept it as a bug of IE.
0
 
niko_edralinAuthor Commented:
I didn't find the solution that I wanted. I'm just going to close this question and split the points to Louis01 and fcardinaux
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now