Downloading a file from the server

  • Thread starter Martin A. Weinberger
  • Start date
M

Martin A. Weinberger

Hi All,

Using JavaScript, how do I download a file to a user's computer that I have
stored on the server in a relatively browser indepdent way? That is will
work on Mozilla and Macintosh Safari browsers just as well as IE?

Thanks in advance,
 
M

Martin A. Weinberger

Hi all,

I solved part of the problem by using:

location.href = "WhatToDownload.ext";

The next question is how to get a notification of when the download
completes.
 
T

Thomas 'PointedEars' Lahn

Martin said:
I solved part of the problem by using:

location.href = "WhatToDownload.ext";

The next question is how to get a notification of when the download
completes.

This is not possible.

Furthermore, the above does not necessarily download the resource as a
file to the local filesystem. One of the following conditions must be
met before that happens:

A) The resource is served with `Content-Disposition: attachment'.
This does not work in Internet Explorer 5.5 SP1:
<URL:http://support.microsoft.com/kb/279667/EN-US/>.

B) The resource is served with an unregistered or unsupported media type
(Content-Type header), or with the media type application/octet-stream
(where the latter would be the correct approach).

C) The resource is served without a media type, or with an unrecognized
media type, or from the name of the resource, particularly its name
suffix, the user agent cannot infer to a supported resource type.
This behavior depends heavily on the user agent and its configuration,
including the installed plugins.

Your code suggests that you rely only on C) which is not recommended.


PointedEars
 
M

Martin A. Weinberger

Thanks for the reply. My needs are simple. I have several text files
(usually CSV) that I want to download via JavaScript to a user's computer.
It can be any function, method, or whatever. I don't care. I just want to
accomplish the task. I googled and came up with the "location.href"
solution. I'm sure that there are hundreds of solutions. That is just what I
came up with so far. Does anybody else have a better idea and how I can get
a notification (event) back, when the transfer completes?
 
T

Thomas 'PointedEars' Lahn

Martin said:
Thanks for the reply. My needs are simple. I have several text files
(usually CSV) that I want to download via JavaScript to a user's computer.

If you are still talking about _file_ download, you cannot do that. The
download, whether being file download or not, is performed by the user
agent, _not_ the J(ava)Script engine. (window.)location, for example,
refers to a host object.

So you can only trigger a HTTP request by accessing a property of a host
object (say, window.location.href). The request is performed by the HTTP
client, and responded to by your HTTP server. If you want file download,
your server has to tell this to the HTTP client in the response (by one
of the means I mentioned before). The client will then display the file
download dialog and perform the download if the user confirms. The script
engine does not have anything more to do with that.

If you are not talking about file download, i.e. not download of the
resource to a file on the local filesystem, you should explain what
exactly you are trying to do, and where it has to work.
[...] Does anybody else have a better idea and how I can get a
notification (event) back, when the transfer completes?

Which part of "This is not possible." in my posting is unclear to you?
There is no such event, at least not cross-browser. The script engine
considers the job done after accessing the property of the host object.

Follow the link to the MSKB I provided, it contains information how you
can accomplish that (without client-side browser scripting, of course).

And please quote the minimum of what you are replying to next time:

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>


PointedEars
 
J

Jonas Raoni

Martin said:
Using JavaScript, how do I download a file to a user's computer that I have
stored on the server in a relatively browser indepdent way? That is will
work on Mozilla and Macintosh Safari browsers just as well as IE?

You don't need JavaScript, just a simple link: <a
href="download.abc?path=file.txt">Download File</a>

And on this "download.abc" you send headers to force the download ;]

Take a look on google:
<URL:http://www.google.com.br/search?q=force+download+php>
 
J

Jonas Raoni

Martin said:
Using JavaScript, how do I download a file to a user's computer that I have
stored on the server in a relatively browser indepdent way? That is will
work on Mozilla and Macintosh Safari browsers just as well as IE?


You don't need JavaScript, just a simple link: <a
href="download.abc?path=file.txt">Download File</a>

And on this "download.abc" you send headers to force the download and
output the file content, it's very simple, take a look on google:
<URL:http://www.google.com.br/search?q=force+download+php>

Bye man, now I'll travel hehe, It's carnaval on Brazil, time to get some
chicks hahaha \o/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top