简体   繁体   中英

I need to generate a JSON file in browser using (currently) javascript

I have a website that allows users to select a date range from a data set. At least, that's the goal.

What I would like to have happen: the user selects a date range, presses the submit button, and a script generates a JSON file which MATLAB reads to generate the graphs.

Any thoughts on resources to help accomplish this?

You'll need the script that fires off to be server side. JavaScript is client side and can not, in any way, access, modify, or otherwise create files on the client. You'll have to use a language like PHP to create the file.

Example using PHP:
Once the file is created, force a request on the client side to fire asking for the file. Set the PHP header to Content-Disposition: attachment; filename="< Place file name here>" Content-Disposition: attachment; filename="< Place file name here>" .

This will prompt the browser to launch a download prompt allowing the user to download the file.

Hope this helps.

You can use Downloadify , a small Flash component with a Javascript interface that allows you to create files on the client that a user can download. That's what I used in a similar situation.

You could also try and use Data URI but they are a quite limited and browser specific so some issues may arise.

These may be alternative solutions to the previous answer that suggested using server side code to generate the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM