I need to generate a JSON file in browser using (currently) javascript - 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>".
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.

Related

Send files without button event

Is it possible to send files without choosing them with button click and searching folder? I have app written in ReactJS that chooses files on button click, but for testing purposes I need to have 'hardcoded' files so that I don't need to make any actions in my application. The question is: can I do something like that within React app?
First of all, you can't read files from the client computer, even if you have the full path. That would be a major security flaw if javascript could access client files without the explicit consent of the user (provided by the browser).
You can, however, create a file within javascript with hardcode content and send it by http request. Here you have a solution on how to achieve this.

How to search inside excel file using HTML/JavaScript form

I have an excel file that contains around 3000 unique ids. I am creating a HTML form where the user will select a code from one of these ids. Similar to something where they click on the code field, which takes them to a new web page where they can either search a specific code or just select to show all and then manually select one code.
I am assuming that I need to use JavaScript or something similar over here to connect the excel file to the form, but I cannot find a specific answer to the question. I have seen a lot of websites like job application websites where they have you search for the university do something similar. But I am not sure where to begin. It would be great if someone pushed me into the right direction.
Javascript on the frontend (in a web browser) does not have access to the filesystem. You should use NodeJS (Javascript Runtime) which does run on the server side, and therefore has access to the filesystem.
Your specific issues can be solved with a simple HTTP GET or POST request to a small NodeJS server which will take the code as a query parameter and proceed according to the requirements you have.
I hope this helps.

Generate .msg file for Outlook

Is there a way to have Javascript/jQuery (or any other web-based language) generate a .msg file that also has an attachment?
I am trying to make it so when a user clicks a button, it downloads a .msg file that has Subject/Body filled out and has a file attached. I know I can just use a long mailto: link but that would not let me attach a file.
You will not be able to pass such content due to both protocol and security measures. Also there is no way to know how each browser is setup to react to the mailto address (Outlook, Webmail, Nothing)..
If you want to send emails with a prefilled subject or content then I suggest you handle it server side with PHP or equivalent language. A great library to consider is SwiftMail
Good Luck

Convert web information to pdf information

I have an internal form that is web-based. Clients insert their information (Name and such) and sign using a signing pad (This is more than an image. It's the speed, pressure, ect of the signature).
I've got this working as PDF's, but the forms NOW need to be web based, and when the clients hit "save" the information they input is converted to PDF. This must be done server side.
Is there a way to program using HTML, JavaScript, or something else to do this?
Or is there a service that will do it automatically on the server?
Nothing can be saved locally.
(Short summary: information fields on local website, client inserts information to fields, signs using signing pad, and all information is sent to server and converted to PDF)
I'm not too familiar with PHP, but could PHP be used to do this? If so, how?
The reason i can't have it client side, is because we're trying to get the whole system to work on Ipads as well as desktop office computers
There are library to build PDF in PHP, for instance fpdf.
In fact there are many libraries out there which allow you to build pdf, you just have to learn their respective APIĀ a little.
Check out TCPDF. It's the PHP library to generate PDFs automatically.
I used FPDF, it's easy to download and the site contains useful tutorials. Good luck
use dompdf. check this link
From here check my answer on this question. You can download dompdf from here

hide the path of the php file in ajax request

I dont know if this possible
But I have an ajax function in my javascript and as all, we know we need to provide php file path in the ajax request and this will be visible to all the users and unfortunately they can see the path of the ajax files and where they are stored .but i dont want this to happen i dont want user or client to see my ajax filed Is there a way to hide this path from the client, from seeing the path of ajax files? i know its something obfuscation but i only want the path to be done is it possible?
Update
so is there any way to stop user from viewing or opening php files from the url, I mean if user enters the path of ajax files and hits enter he can see all my files over there and he can easily hack my data so is there a way to stop user from entering them , i want only my pages to access php files not from users url,Thanks
It is not possible. The browser has to request the URI. The user can see what resources their browser is requesting.
"Ajax files" are not a special case. You need to secure them in exactly the same way as any other resource you provide over HTTP. i.e. use authentication and authorisation to make sure that only users who are allowed to access them do, input validation to make sure that any data sent to them is acceptable, and escaping to make sure that the data doesn't cause you problems if you try to insert it into SQL/HTML/JS/any other data format.
Response to update:
so is there any way to stop user from viewing or opening php files from the url
Still No!
I mean if user enters the path of ajax files and hits enter he can see all my files over there
You can configure your server to not serve a DirectoryIndex. The specifics depend on the server. Alternatively you can put a blank index.html file there.
and he can easily hack my data
How?
so is there a way to stop user from entering them
No.
i want only my pages to access php files not from users
Only an HTTP client can request a URI. There is no way to distinguish between an HTTP client responding to an instruction from JavaScript that you have written from one that is responding to user input.
a possible way would be to obfuscate it so that its not user readible. expl; by using a main script and including the file by switching on some POST/GET param your sending (could be a random string, hash, number).
more efficient obfuscation can be acchieved, when you would redirect all traffic to one script via mod_rewrite. Somewhere in the url, you hide a number, that tells you which file to use. Then you add random string before and after it (with specific length) and read it from your main script. But then the user will find out by looking at the javascript.
but srsly. why do you care? there is no security by obscurity!
/updated to your updated question;
I think you want to be looking at your apache configuration; use .htaccess files or better, disable indexing of a directory all together. The user wont be able to know about all your php files. Only the ones, you made public via links in your application. There is no such thing as "only letting ajax access file but not the user". because ajax IS the user and vice-verca! you are sending ajax request on behalf of the user! and if the user enters the url in the browserbar, its the same as sending an ajax request.
You could add a special header with your ajax call (or use the one already supplied), but that wouldn't change anything, as soon as the user looks at the traffic close enought.
What I think you want is a User Session to only allow privileged users to fully execute a script.
Unless you are using jsp technology with apache tomcat, you can place your folders in the WEB-INF.
No.
This is not possible. The client performs the request and the client is under the users control.

Categories