Content-disposition Behavior with Javascript

I was wondering if it is possible to make the browser behave the same as when viewing "Content-disposition: attachment; filename = ..." using only client-side javascript? This means that the data for the file to be saved is available only on the client side.

those. Suppose we have an array of JavaScript, only on the client side,

var data = [ ["aa","bb","cc","dd","ee","ff","gg","hh","ii"] [ 1, 2, 3, 4, 5, 6, 7, 8, 9], .. ]; 

and I want to save this array as a text file to the user's computer. The user should receive a request for the file name (i.e. I am not trying to bypass the browser security settings or something like that).

Is it possible to do this without storing the array in a temporary server file and making another request to return this temporary file to the user? If there is no simple answer to this question - any ideas, Google keywords or links are very much appreciated.

+4
source share
4 answers

You cannot do this with a pure JavaScript solution - you need the file to be clicked from the server.

But for this you do not need to have a โ€œphysicalโ€ file on the server. You can store your data string in memory and write to the response stream from memory. But you will not receive a more detailed answer on how to do this without telling us which server technology you are working with.

+1
source

AFAIK, you cannot do it well using Javascript in any kind of browser.

If you really don't want to use a server, you can create a hybrid solution using Flash. Essentially, you would create a custom Flash control that you could interact with through Javascript (ExternalInterface), and then the Flash control would initiate a file save operation.

Here's a related article: Save a file locally using Flash Player 10

+1
source

Take a look at dowloadify : it uses the technique suggested by @Adam

+1
source
 var YourTextData = "text data here"; window.location.href = "data:application/octet-stream," + encodeURIComponent(YourTextData); 
+1
source

Source: https://habr.com/ru/post/1347173/


All Articles