5

Possible Duplicate:
Force download of ‘data:text/plain’ URL
Create a downloadable link from a vriable in javascript

I have seen plugins that can create/generate files (.txt, .pdf, .vcard) and I'm not exactly sure how it's done. Are these done through Ajax? Or is there an object/method I'm missing?

Community
  • 1
  • 1
Dandy
  • 851
  • 2
  • 11
  • 22
  • Is it possible to do this only using client side? – Dandy Dec 21 '12 at 20:03
  • Only with FileSystem API but I am not sure thats what you are looking for... Do you have a site with an example? – Andrew Hall Dec 21 '12 at 20:04
  • 2
    In order to create files on the user's computer, you generally create a binary string with the file data and then redirect the page to a [`data:` URL](https://en.wikipedia.org/wiki/Data_URI_scheme) with the base64-encoded file data, so the browser can download it. – apsillers Dec 21 '12 at 20:05
  • @adeneo, your statement "you'll need ajax, as javascript" makes no sense. Javascript is what drives AJAX. – Dmitry B. Dec 21 '12 at 20:06
  • Here are some examples: http://badassjs.com/post/708922912/pdf-js-create-pdfs-in-javascript https://github.com/mattt/vcard.js – Dandy Dec 21 '12 at 20:08
  • Maybe you should expand if you want to create the files on the server side or on the client computer's filesystem. – crackmigg Dec 21 '12 at 20:10

2 Answers2

1

Html5 does support access to File System. Below link has a good example:

http://www.html5rocks.com/en/tutorials/file/filesystem/

malkassem
  • 1,937
  • 1
  • 20
  • 39
  • Nice! This is very helpful, but I'm curious how people have been able to generate files before HTML5 was available. Also, since this isn't widely supported yet. – Dandy Dec 21 '12 at 20:13
  • Generating file *data* and prompting the creation of an *actual file* are two separate steps. Direct your browser to `data:text/x-plain,helloworldfoobar` and you'll see it's quite easy to prompt for a download, once you have your file data created. Generating file data is roughly the same as generating any kind of data in any language: move some bits and bytes around until they look how you want. – apsillers Dec 21 '12 at 20:17
  • On a microsoft machines, via VBScript for example, you can do the following: Set fso = createobject("scripting.filesystemobject") ... that only worked on ie. I think there was a similar way to do so in javascript. – malkassem Dec 21 '12 at 20:20
0

Here is the spec for the FS API. Unfortunately support for it is very light. Though, if you want to use it in mobile apps, PhoneGap has implement it.

If you write a GreaseMonkey script you have GM_xmlhttpRequest which doesn't have the same origin policy.

Adrian Ber
  • 20,474
  • 12
  • 67
  • 117