Js Download File From Url

Complicated code and it is a server side file download, you can do it with single line in javascript by window.open(url,'). Re: Actually it a server side file download and not javascript ashishjain88 4-Aug-11 22:12.

  • Npm install download-file --save. Var download. Url string of the file URL to download. Options object with options. Directory string with path to directory where to save files (default: current working directory) filename string for the name of the file to be saved as (default: filename in the url).
  • JQuery File Download is a cross server platform compatible jQuery plugin that allows for an Ajax-like file download experience that isn’t normally possible using the web.
Active5 years, 4 months ago

Trying to initiate a browser download in Javascript, but the data I want to be downloaded is in a string, not a file. I know if it were a file, the following would do it:

How can I get this same effect, only with a string (with csv data), not a file that already exists on the server?

Joe BergevinJoe Bergevin
2,0623 gold badges20 silver badges31 bronze badges

2 Answers

using my handy downloader:

you can do it without a library as well, though my 'lib' isn't very big and supports older FF+CH and IE10:

EDIT: the linked script now supports window.URL.createObjectURL() for downloading files that were too big using dataURLs. I don't know the new limit, but 10mb works just file, whereas ~2mb is a limit for many dataURL ( window.open/A[download] - based ) solutions3

dandavisdandavis
12.7k3 gold badges28 silver badges32 bronze badges

Below is a function I have writen in the past to handle such behavior (it may require some tweaking):

malkassemmalkassem
1,7591 gold badge14 silver badges37 bronze badges

Not the answer you're looking for? Browse other questions tagged javascript or ask your own question.

Active16 days ago

I'm playing with the idea of making a completely javascript based zip/unzip utility that anyone can access from a browser. They can just drag their zip directly into the browser and it'll let them download all the files within. They can also create new zip files by dragging individual files in.

I know it'd be better to do it serverside, but this project is just for a bit of fun.

Dragging files into the browser should be easy enough if I take advantage of the various methods available. (gmail style)

Encoding/decoding should hopefully be fine. I've seen some as3 zip libraries so I'm sure I should be fine with that.

My issue is downloading the files at the end..

this works fine in firefox but not in chrome.

I can embed the files as images just find in chrome using <img src='data:jpg/image;ba..' />, but the files wont necessarily be images. They could be any format.

Can anyone think of another solution or some kind of work around?

Tim S. Van Haren
8,2992 gold badges26 silver badges34 bronze badges
MikeeMikee
9231 gold badge10 silver badges16 bronze badges

10 Answers

Ideas:

  • Try a <a href='data:....' target='_blank'> (Untested)

  • Use downloadify instead of data URLs (would work for IE as well)

Pekka 웃

Download Audio File From Url

Pekka 웃
368k120 gold badges866 silver badges1026 bronze badges

If you also want to give a suggested name to the file (instead of the default 'download') you can use the following in Chrome, Firefox and some IE versions:

And the following example shows it's use:

owencmowencm
4,7694 gold badges22 silver badges44 bronze badges

or:

ZibriZibri
4,8632 gold badges30 silver badges32 bronze badges

Want to share my experience and help someone stuck on the downloads not working in Firefox and updated answer to 2014.The below snippet will work in both firefox and chrome and it will accept a filename:

Php Download File From Url

Martino DinoMartino Dino

There are several solutions but they depend on HTML5 and haven't been implemented completely in some browsers yet. Examples below were tested in Chrome and Firefox (partly works).

  1. Canvas example with save to file support. Just set your document.location.href to the data URI.
  2. Anchor download example. It uses <a href='your-data-uri' download='filename.txt'> to specify file name.
Viacheslav DobromyslovViacheslav Dobromyslov

Here is a pure JavaScript solution I tested working in Firefox and Chrome but not in Internet Explorer:

Cross-browser solutions found up until now:

downloadify -> Requires Flash

databounce -> Tested in IE 10 and 11, and doesn't work for me. Requires a servlet and some customization. (Incorrectly detects navigator. I had to set IE in compatibility mode to test, default charset in servlet, JavaScript options object with correct servlet path for absolute paths...) For non-IE browsers, it opens the file in the same window.

download.js ->http://danml.com/download.html Another library similar but not tested. Claims to be pure JavaScript, not requiring servlet nor Flash, but doesn't work on IE <= 9.

BBaysinger
2,1975 gold badges33 silver badges77 bronze badges
Jesus M CJesus M C

Combining answers from @owencm and @Chazt3n, this function will allow download of text from IE11, Firefox, and Chrome. (Sorry, I don't have access to Safari or Opera, but please add a comment if you try and it works.)

kevinarpeOnlinekevinarpe
11.2k16 gold badges92 silver badges118 bronze badges

One may also initiate data URL downloads via JavaScript. See https://stackoverflow.com/a/13696029/271577 for such a solution (along with text link examples).

Community
Brett ZamirBrett Zamir
10.1k3 gold badges33 silver badges58 bronze badges

For anyone having issues in IE:

Please upvote the answer here by Yetti:saving canvas locally in IE

Community
Chazt3nChazt3n
1,0973 gold badges12 silver badges37 bronze badges

Your problem essentially boils down to 'not all browsers will support this'.

Html Download File From Url

You could try a workaround and serve the unzipped files from a Flash object, but then you'd lose the JS-only purity (anyway, I'm not sure whether you currently can 'drag files into browser' without some sort of Flash workaround - is that a HTML5 feature maybe?)

Download File Using Url

PiskvorPiskvor

How To Download File From Url

73.9k41 gold badges159 silver badges210 bronze badges

Node Js Download File From Url Https

Not the answer you're looking for? Browse other questions tagged javascriptdata-url or ask your own question.

Comments are closed.