Jack
Jack

Reputation: 75

Provide file download webpage without server side code

Lets say i have this string "something1,something2" and i want to download it as "text.csv", without opening new window (pop up) how could i do this from a webpage. can i dot it in JS without using this:

 window.open('data:text/csv;charset=utf-8,' + str);

or do i have to use PHP for this ?

Upvotes: 0

Views: 312

Answers (2)

CaffGeek
CaffGeek

Reputation: 22064

While limited, there is also http://en.wikipedia.org/wiki/Data_Uri

So this as a url would open an image

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==

You could have javascript open this url to trigger the download

A CSV example

data:text/csv,field1%2Cfield2%0Afoo%2Cbar%0Agoo%2Cgai%0A

Just copy/paste either of those into your browser to see them work. Browser support is limited.

IE 8 for example has a 32KiB limit

Upvotes: 0

nikmd23
nikmd23

Reputation: 9103

What you want to do is a bit unconventional, but it is possible.

Take a look at Downloadify. It's a JavaScript library that leans on Flash to create a file on the client side and present the file download dialog.

David Walsh has some good demos and info too on his blog.

Upvotes: 1

Related Questions