473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Downloading a file from the server

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,

--
Martin A. Weinberger
ButterflyVista
If you want to contact me by email, then please remove "xxxx_".


Feb 18 '06 #1
6 2195
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.

--
Martin A. Weinberger
ButterflyVista
http://www.butterflyvista.com/
Feb 18 '06 #2
Martin A. Weinberger wrote:
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
Feb 18 '06 #3
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?

--
Martin A. Weinberger
ButterflyVista
http://www.butterflyvista.com/
Feb 18 '06 #4
Martin A. Weinberger wrote:
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
Feb 18 '06 #5
Martin A. Weinberger wrote:
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>
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Feb 25 '06 #6
Martin A. Weinberger wrote:
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/
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Feb 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ASP Spam Fighter | last post by:
Hello all, I don't know how to get around this one... If anybody can help me with this problem, I would appreciate it very much. I've been trying to send a (large) file to the browser via a...
6
by: Shawn | last post by:
Hi. How can I download a file and store it on the web server. I have a complete URL to the file, but I never know what kind of file it is. It can be pdf, jpg, tif, doc, xls etc. Thanks, Shawn
4
by: Joe | last post by:
I'm hosting my web service on a Windows 2003 box which is remotely located. When trying to add a web reference to a C# project I get an error message 'There was an error downloading...
4
by: Richard L Rosenheim | last post by:
I know that I can download a file from a web server by using the WebClient.DownloadFile method. But, does anyone know of an example of downloading a file from a web server with the ability to...
1
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
3
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
2
by: Tomas Martinez | last post by:
Hi there! I'm trying to download a file in my asp.net web, but when downloading it from a Firefox browser, instead of downloading the example.exe file, it's downloading example.exe.htm. My code...
5
by: rony_16 | last post by:
Hi, I have a problem downloading a file . after i connect to the website and get the stream , i treing to write the file on the HD. public void SaveStreamToFile(string filePath, Stream stream) {...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
1
by: Muddasir | last post by:
Hello everyone. I am facing problem in downloading .xls file. I generate report and save data in excel sheet on server. and once user click the 'save data in excel format', an excel sheet is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.