Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    8
    Rep Power
    0

    Access is denied (JavaScript error) while writing to the stream to download a file


    I wrote a servlet that would let a user download an XML file. The servlet calls a utility class passing it the HttpServletResponse object. The utility class then writes then writes the XML file to the ServletOutputStream corresponsing to the HttpServletResponse object. Before I write it, I perform the following steps.

    res.setContentType("application/stream");
    res.setHeader("Content-Type", "attachment;" + "filename=" + filename + ";");

    Then I wrte to the ServletOUtputStream. This works fine in Netscape and IE. The browser opens up a save as dialog box, and the user can save the file as the name specified above (filename).

    Now the problems comes in when suddenly after the user downloads and saves the file, none of the Javascript that is called on events (onclick, onchange) that access any of the documents objects on the screen (elements) work in IE. I receive a JavaScript error in IE which says "Access is denied." If the user does not save the file and clicks cancel everything works fine. However, if I call JavaScript which access these same document objects from a "href" everything works fine and then so does all the other JavaScript. Its almost as if it refreshed some settings of the page.

    Also, if I comment out the 2nd line above (res.setHeader()) it will download correctly. However it will use the servlet name and some random number as a default to save as, and it does not default to saving it as an XML document. But then all the JavaScript works fine.

    My ultimate goal is to save the XML file defaulting the "save as" dialog box with a name I provide and as an XML document so the user doesn't really have to do anything. And then I still want all the JavaScript to work since I perform client side validation.

    This has been a big problem to me. I hope someone can help.
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    118
    Any chance we can see the links to the page and/or some code?

    Also, I believe the correct mime type for an unspecified binary file is application/octet-stream

    I also just noticed that you are using content-type as the header you are specifying the filename in. I believe that should be Content-disposition.
    Last edited by Nemi; February 1st, 2003 at 06:11 PM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    2
    Rep Power
    0
    Hi,
    I'm experiencing the same problem. Have you ever figured out a solution? Thanks
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    2
    Rep Power
    0
    figured it out....use
    resp.setHeader("Content-Disposition","inline; filename=\"" + file.getName() + "\"");

    inline replaces attachement is the key
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    8
    Rep Power
    0
    Hey, i have the same problem. so i tried putting the inline instead of attachment, but now it opens the file directly in excel without asking whether i want to view it or save it. and it doesn't even recognise the file properly. it's a csv file with colums seperated with comas, and yet Excel opens it all in one colums. the file doesn't even have a name, and when i try to save it it doesn't give a name either... is that normal?

    thx
  10. #6
  11. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    118
    Usually if you want to prompt the user for what they want to do with the file, you use the word "attachment" instead of "inline". inline will try and open it in the browser normally (or whatever is setup to open that type of file).
    Last edited by Nemi; October 10th, 2003 at 09:06 AM.
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    8
    Rep Power
    0
    yeah that's true, but the problem is, if i use attcahment i get an access denied error after saving the file. it's the same problem that svelez has.
  14. #8
  15. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    118
    Ok, I think I know of this problem. I think it is a known IE problem. I assume you download this file using a link? If so, what does it do if you provide a different target for the link? Something like target="_blank"?
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    8
    Rep Power
    0
    I have to leave right now, but i'll try it on monday and let you know what happens. thx for ur help!
  18. #10
  19. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    118
    Ok, I don't see the actual thing you click on to initate the download. Is it a link, button, graphic? I assume it is a button because of the ChangeButton function, however, I don't see this function listed anywhere. Realize that if you don't post all the relevant code I cannot give you good answers.

    Having said that, it looks like it initiates the file download by setting the location of the document. You are going to have to change the way this is done. Either use a link or a hidden form. I would suggest a hidden form. You can get the form by name in the same function that currently initiates the download and submit it. You can set the action and the target of the form.

    I am not saying this is guaranteed to work. I am merely suggesting it as a possible solution. I have not tested it.

    If you set the target to a new, non-existing window, a new window should open, but I believe that if a file save dialog comes up the new window closes automatically.
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    8
    Rep Power
    0
    I actually figured it out! like u guys said it's a known IE problem and i had to work around it. I used the IFRAMEs like it says on http://support.microsoft.com/default...;en-us;Q306673 and it works! it was a bit annoying tho cos the iframes work only with IE and not netscape so i had to do them seperately.
    Anyways, thx alot for the help Nemi and everyone!
    Last edited by Queen_Amidala; October 16th, 2003 at 02:15 AM.
  22. #12
  23. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    118
    Thanks for posting the fix!
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
  24. #13
  25. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    8
    Rep Power
    0
    Has anyone found another solution, one that is browser-independent?

    Thanks!
  26. #14
  27. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    8
    Rep Power
    0
    Nope sorry, but that's the only solution I found (I looked for a long time!). But Can u not just treat them seperately? I mean, in ur javascript code u use an if close (if IE do sth, else if Netscape do sth else)...
  28. #15
  29. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    118
    ...Content Removed...

    EDIT: I had a suggestion, but I realized it would not work. I also second Amidala's idea of using an if statement.
    Last edited by Nemi; October 22nd, 2003 at 02:17 PM.
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo