tags:

views:

113

answers:

3

I want to implement dynamic, client side file generation in javascript. Is it possible? For example, a user pushes a button and the script suggests to download the generated file.

+3  A: 

Not possible without a server side script that will return a response of the proper mime-type.

Zoidberg
Eran Betzalel
Sorry, let me rephrase that... NOT PRACTICAL. Why go through all the trouble to do it client side when a simple server side solution will do. What about firefox, Opera, Chrome, Safari etc. Can you guarantee it will work on all these. Sometimes being able to do something doesn't mean it should be done.
Zoidberg
It's not recommended - correct, but maybe it's a must for the system he plans. Anyway, it's not your duty to decide that - its his only.
Eran Betzalel
I would be curious to see why a company would want to implement such a solution. And as a developer why they wouldn't be put straight on this issue. One of the worst things you can do is to say "YES i can do this" when its plainly a bad idea. I think the best solution would be to tell your boss no can do!
Zoidberg
Security fanatic government institutes would - I assure you...
Eran Betzalel
I am not deciding anything for him. I think the point of stack overflow is provide the best solutions to every problem and let the person decide which he feels works best. ANd in this case, he has.
Zoidberg
+1  A: 

It depends a bit on what kind of file. You can generate an HTML file by opening a window and writing to it; the user can then download it via File | Save As on most browsers.

A text file can be generated and loaded into a textarea element, and then the user can either manually copy the file, or there are various ways to copy it to the clipboard (you could even keep the textarea hidden and just offer the copy button). Search for "javascript clipboard" for various ways to do that; here's one of the first lnks that comes up for that part of it.

Binary files will probably need to be handled server-side.

T.J. Crowder
A: 

You can always use FSO to generate a text-oriented (binary won't work) file on the client's computer, return the link and download it.

Be aware that it will only work for IE (ActiveX) and you will be asked for more security rights.

Good luck.

Eran Betzalel
Two things: 1) That's a Windows-only solution, and 2) Even in IE, it will only work if you have enhanced security rights.
T.J. Crowder
That's right. He didn't specify any specific "wants" - so I figured that everything goes and gave my best solution.
Eran Betzalel
I agree with T.J. Delivery of such a solution is impractical, and will cause more head aches in the long run than doing it the proper way with a server side solution. Its also a guarantee that most users will not know how to change their security settings.
Zoidberg
Who are you to decide whether it's practical or not? Do you know the person who asked the question? Do you know for what cause he might need it? NO, You don't. You're right, using FSO is not recommended, but answering **NO, YOU CAN'T** is the worse answer you can give him.
Eran Betzalel