最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - "Content-disposition"-like behavior with Javascript - Stack Overflow

programmeradmin1浏览0评论

I was wondering if it is possible to make browser behaving the same way as it does when sees "Content-disposition: attachment; filename=..." by using client-side javascript only? This implies the data for the file to be saved is available on the client side only.

I.e. suppose we have a javascript Array, on client side only,

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 users's puter. The user must be prompted for the file name(i.e. I'm NOT trying to bypass browser's security settings or anything like that).

Is this doable without storing array to the server's temp file and making another request to get this temp file back to the user? If there is no simple answer to this question - any ideas, google keywords or links are much appreciated.

I was wondering if it is possible to make browser behaving the same way as it does when sees "Content-disposition: attachment; filename=..." by using client-side javascript only? This implies the data for the file to be saved is available on the client side only.

I.e. suppose we have a javascript Array, on client side only,

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 users's puter. The user must be prompted for the file name(i.e. I'm NOT trying to bypass browser's security settings or anything like that).

Is this doable without storing array to the server's temp file and making another request to get this temp file back to the user? If there is no simple answer to this question - any ideas, google keywords or links are much appreciated.

Share Improve this question edited Apr 7, 2011 at 21:22 Pointy 414k62 gold badges595 silver badges629 bronze badges asked Apr 7, 2011 at 20:56 VeraVera 511 silver badge6 bronze badges 3
  • can i propose saving this array in a cookie ??? if its wele i can bring you the code :) – Marwan Commented Apr 7, 2011 at 21:06
  • No, this needs to be a real file in the user's file system, so the user can open it later and possibly edit it's content, etc. – Vera Commented Apr 7, 2011 at 22:08
  • Cookie simply wouldn't do - they are available from the browser only. I cannot tell the user to go Tools->Options->Privacy->Manage Cookies to get his data. – Vera Commented Apr 7, 2011 at 22:09
Add a ment  | 

4 Answers 4

Reset to default 1

You can't do it with a pure JavaScript solution - you need to have the file push e from the server.

But you don't have to have a "physical" file on the server to acplish that. You can store your data string in memory and write to the response stream from memory. But you won't get any more detailed answer on how to do that without telling us what server-side technology you're working with.

AFAIK, you cannot do this well using Javascript in any sort of cross-browser manner.

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

Here's an article on the topic: Save a file locally with Flash Player 10

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

var YourTextData = "text data here";
window.location.href = "data:application/octet-stream," + encodeURIComponent(YourTextData);
发布评论

评论列表(0)

  1. 暂无评论