473,666 Members | 2,517 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2205
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.microso ft.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.h ref"
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.)locati on, 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#ps1P ost>
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.t xt">Download File</a>

And on this "download.a bc" 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.t xt">Download File</a>

And on this "download.a bc" 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
3217
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 download page that uses a file download component. The file downloads fine, except that Internet Explorer refuses to browse to a different page while the file is downloading! I don't understand why it does that and I don't know how to fix this...
6
307
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
2928
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 'http://mydomain.com:port/webservice.asmx' The operation has timed-out (I've tried with and without using a separate port for the service) The weird thing is the page does show up on the left side of the screen listing the available methods but the Add...
4
3876
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 monitor the download progress? I'm looking for either the number of bytes downloaded and/or percentage completed. TIA
1
2069
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 any page from the server while downloading. If I try to call a single page while downloading a file then the page request goes time out and the server then closes the existing download stream and the client doestn't throw any exception. So many a...
3
2786
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 any page from the server while downloading. If I try to call a single page while downloading a file then the page request goes time out and the server then closes the existing download stream and the client doestn't throw any exception. So many a...
2
2769
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 is the following: string localfile = MyComponent.DownloadMyExe(index); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType="application/octet-stream";
5
1852
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) { // If not all data was provided return false; //if (string.IsNullOrEmpty(filePath) || stream == null) // return false; // System.IO.Stream gzStream = stream; //= new
35
9345
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 500000 to 3200000 of a file whose size is say 20MB... how do i request a download which starts directly at 500000th byte... thank u cheers
1
1678
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 been created on the server.once created then user can download that file. i am able to download file but the problem is that when user apply sorting to data and then save data in excel sheet. the file on the server contains the data in the same...
0
8454
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8362
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8878
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8785
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6200
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5671
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.