Jump to content

JS file download


justsomeguy
 Share

Recommended Posts

I'm trying to set up a Javascript function that will let the user download a dynamically generated file, so the URL goes to a server-side script. Does anyone know of a way to have the open/save dialog box appear without showing a blank popup window? Right now I have window.open opening a small window to the script, which causes the download box to appear, but the window stays open and blank. Does anyone know of a way to achieve the same thing without needing the popup? If I put a call to window.close right after the window.open then the download box won't even show up, and I don't want to set a timeout to close the window because I can't guarantee how long it will take to build the file.

Link to comment
Share on other sites

Well, it's not standard, but maybe you can create an <iframe> (with javascript), make it invisible with CSS and load the file in it.I don't really like it much, but I think it will work.I can't think of a better solution right now.

Link to comment
Share on other sites

The way we took care of this on our site was to set the "content-disposition" header to "attachment; filename=FILENAMEHERE".The way ours works is that there is a form and the page gets submitted to itself, but this should work for any request - not just posts. Then, the server writes that header in the response, sets the Content-Type header to the appropriate MIME type and then proceeds to write the contents of the file to the response.

Link to comment
Share on other sites

Got it worked out, I ended up using an iframe (I didn't want to on my nice HTML 4 strict page, but what the ######). I tried that before with CSS display set to none and it wasn't working. I tried it again modelled off the iframe at sourceforge (width=0, height=0) and it worked. I changed one other thing on the backend, I enabled caching for the dynamic file, but I don't know if that made a difference or not. IE6 had issues trying to open an "Office" document (e.g. CSV) over HTTPS with caching disabled, it said it "couldn't download the file". Enabling caching solved that issue, but sticking an iframe there and just updating the src attribute of it got what I wanted. Apparently Firefox and IE7 were working as wanted anyway, not leaving open a 100x100 window that pointed to a file that it couldn't display, but I was testing in Opera and IE6 which both left it open.@jesh: I've got the script file outputting the CSV setting the content-disposition header as well, but this is an AJAX application so reloading the page wasn't an option. I just have an Export button you can click to export a big table as CSV data, so I needed to have Javascript just show the download box without opening a window. I wish the iframe was part of HTML 4 strict, or that object worked for this (which it doesn't).Thanks for the suggestions.

Link to comment
Share on other sites

OK, I just ran a test - in Firefox and IE - that allows me to download a file without having to run any post-backs to the server, doesn't require a page refresh, and doesn't require an iframe. It looks like this:ASP.NET ASPX Page (essentially empty)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DownloadTest.aspx.cs" Inherits="SandBox.DownloadTest" %>

ASP.NET ASPX Code Behind

using System;namespace SandBox{	public partial class DownloadTest : System.Web.UI.Page	{		protected void Page_Load(object sender, EventArgs e)		{			Response.AppendHeader("content-disposition", "attachment; filename=jesh.pdf");			Response.ContentType = "application/pdf";			Response.WriteFile("test.pdf");			Response.End();		}	}}

TEST HTML Page

<html><body>This is a test page.<br /><br /><a href="http://localhost/sandbox/DownloadTest.aspx">Download PDF</a></body></html>

When I click on that link, the Open/Save dialog opens immediately and the content of the page does not change.I hope this helps.

Link to comment
Share on other sites

But isn't that just the same as doing

<html><body>This is a test page.<br /><br /><a href="test.pdf">Download PDF</a></body></html>

Link to comment
Share on other sites

Yes, except that there is a Response header that has been set to:content-disposition=attachment; filename=jesh.pdfAnd, rather than the browser changing the location of the page and loading the new file, in this case, it simply opens the Open/Save dialog window.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...