#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 am having some trouble allowing a user to download an XML file from a servlet by popping up a "Save AS" dialog box to allow them to save it where they want. The XML file is dynamically built and then write to the OutputStream. However, once the user clicks save, and saves the file, I receive a "Access is denied" JavaScript error in IE 5 while try to perform any mouse event function on a form object on the html page.

    I use zn HTML template and after I write to the XML file to the stream I do not re-print the template because I do not want the code of the template to end up in the XML file.

    Here is the code I use.

    res -- HttpServletResponse object

    bout -- is a ByteArrayOutputStream that I write to the XML file to until I know I'm ready to write it to the stream.

    filename -- is the name of the file i want to name it.

    res.setContentType("application/stream");
    res.setHeader("Content-Disposition",
    "attachment; filename=" + filename + ";");
    out.write(bout.toByteArray());
    bout.close();
    out.flush();

    Now I don't set the content type to "text/xml" because then IE will attempt to render it directly to the browser since it recognizes what an XML file is.

    This works fine in Netscape 4.7, but it fails in IE 5. I receive an "Access is denied" Javascript error. I thought it may have been because it got confused on the origin of the Javascripts source but this doesn't work on a local machine, 1 server, or multiple servers on a load balancer. Again this error only occurs on any function called on event handler that tries to access the document properties, such as document.forms[0].text.value. This page uses mutiple extrenal JavaScript files.

    If the user decides to click "Cancel" instead of "Save" on the "Save As" dialog box, all the JavaScript works. It's only fails in IE5 and when the user actually clicks save.

    I would appreciate any help. Thanks
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Well, quick fix, try targeting you download form to "_blank". If that doesn't work, can you post some more code (the html page, maybe)? This doesn't sound like its your java code that's causing the problem.
    -james
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    8
    Rep Power
    0
    I changed the target on the form to target="_blank" and all it did was open it in another window. This is no good, because I currently use frames and the middle contains this page to download from. Plus any action I called on this page, even if it isn't for a download it will open it in a new window.

    <body class="pgSetup" onLoad="init();">

    <form NAME="Servletname" METHOD="post" ACTION="/servlet/form/Servletname" onSubmit="return validate(this);">

    <INPUT class="input" TYPE="text" NAME="id" SIZE="4" MAXLENGTH="4" VALUE="" ONCHANGE="validateField(this,' ')">

    I receive the "Access is denied" Javascript error when the onchange event is triggered on the above object. The js function is in an external file outside of the parent directory for which this servlet is located. In turn this function calls a function within another external js file that IS located in the same parent directory as this servlet. The function performs validation and therefore tries to access the document.forms[0].id properties and this is where the error occurs.

    Hope this helps. If you need anything else let me know.
  6. #4
  7. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Well, nothing is jumping out at me, except for maybe the frameset part. An access denied error is usually caused by cross-site scripting. This would mean that one page in your frameset would have to be coming from a different server (a different hostname or port is all it takes). If you try calling javascript from a page on one site to a page on another site, you'll get that error.

    I don't see that happening from what you've posted, but double check that all of your frames have pages loaded from the same server (hostname and port).

    Also, it shouldn't make a difference where your javascript files are. They could be on a server in Madagascar and it should still work.
    -james
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    8
    Rep Power
    0
    In development this web application runs on 1 web server, Yet I still get this error. I thought of that, but it doesn't seem to make sense. Even if that was the case it wouldn't work before I try to download a file. The JavaScript always works unless the user tries to download a file. Also, remember that if the user decides to press 'Cancel', instead of 'Save As' the Javascript will work fine. This is a complete mystery to me.

    I am desperate for any help. Thanks
  10. #6
  11. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Well, can you post some more of your code? I'm still not exactly sure how your application is setup. How exactly the javascript in the form relates to the download, specifically.
    -james
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    Montreal
    Posts
    3
    Rep Power
    0

    Exclamation


    svelez,
    I have the same problem.
    Did you have solution for this problem. I appreciate your help.
    Thanks a lot.
    Sabeur.
  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
    117
    I would suggest for a possible workaround that you might want to try opening a new "download" window that allows the user to download the file from there instead.

    So the order of operations would be:
    [list=1][*]User clicks on button (or whatever) to say it wants to download the xml file.[*]Open new window and load a "download" page that requires the user to click a button or link to download the file.[*]This download window closes (think of it as a dialog window) and the user is prompted to download the file as usual. [/list=1]
    This adds one redundant step to the process, but may get you around your problem. Basically, since downloading the file seems to screw up the IE window where the download was initiated from, maybe openeing and performing the operation in a new window will keep this from happening. Hope this helps.
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Chicago, IL
    Posts
    2
    Rep Power
    0
    I too am experiencing the same problem, except that my server-side resource is Perl. We batch produce Excel reports for many of our clients and deliver them via the web. Our clients grab their reports via a HTML form that has a wizard look and feel, i.e. 1. select a, 2. select b, etc. When the users clicks the "download" button, the window's location is set to the Perl script and a CGI string with the selected parameters.

    As was previously described, if the user actually saves the file, I receive an "Access is denied" error. This is coming from my JavaScript whenever it tries to access document or even window parameters. I cannot even get window.name or document.name to work.

    We are noticing this problem with IE 5.0, 5.5 and I believe 6.0. I believe our web server is Apache. The really weird thing is that the methods described above used to work as of a couple of weeks ago. We haven't undertaken any browser upgrades that I'm aware of. What is also weird is that opening the document in the browser doesn't seem to cause the problem. It is almost as if the document's contents need to change if the location changes. If not, all references are lost and hence the "Access is denied" error.

    Does anyone else have any thoughts?

    Thanks in advance...
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Chicago, IL
    Posts
    2
    Rep Power
    0
    Sabeur was kind enough to e-mail me with the below IE5.5 bug report. I'm forwarding it on along with some other workarounds that I've ended up using to solve the problem.

    The problem with JavaScript becoming "disabled" after downloading content seems to be a known IE 5.5 bug. The Microsoft solution is to use a hidden IFRAME and have your download occur from that frame instead of the main display frame (http://support.microsoft.com/default...b;en-us;306673). Thanks Sabeur!

    Another option that I've come up with would be to have a hidden applet sit on your page and have it open up a URL connection to the server and then show the "document" returned. I've used this quite frequently to grab data from the server and refresh the page via DIV tags w/o having to re-draw the entire window. The applet will only be about 6K so the download/footprint is negligible. The "showDocument()" method will trigger IE and presumably Netscape (haven't tested it) to open up the download window.

    // Define a URL object representing the applet
    URL appletURL = this.getDocumentBase();

    // Parse the applet's URL to get the full address string
    String perlURL = appletURL.getProtocol() + "://" +
    appletURL.getHost() + appletURL.getFile();

    // Hack off the HTML page name from the URL and add my perl
    // script file name and any CGI parameters that I wish to
    // pass that are set in strURLString
    perlURL = perlURL.substring(0, perlURL.lastIndexOf("/"));
    URL fileURL = new URL( perlURL + "/report_intrf.pli" +
    strURLString);

    // Execute connection and show contents to browser.
    this.getAppletContext().showDocument(fileURL);

    Of course you would need a bit more in your code, but this would essentially be the working guts of it.

    A third option is to have a new window open which causes the download. It avoids the error, but the user is left having to close the window unless you do some fancy onBlur type of stuff - which might stop the download if the user clicks away from the window.

    I hope this answers people's question. Please feel free to contact me should you have any questions.

IMN logo majestic logo threadwatch logo seochat tools logo