Uploading a file to a perl cgi script from Javascript/jquery - javascript

I want to send a file to a server using Javascript on the client end and a perl cgi script on the server. And well I don't know where to start.
This is what I understand that I need to do; open the file read it, pass it to the server as the request body, pull the file out of the request body at the server end.
So any one know a good way to do this? also if I have got any of the basics wrong please let me know.

I'd recommend using jquery client-side and CGI.pm server-side. The following resources will provide details:
Tutorial on how to exploit the File API of modern browsers (with or without drag-and-drop metaphor).
To see whether or not your targeted user agents support this technique, look here and here.
JQuery method to issue a POST request
the data attribute will be reader.result in your case (adopting the variable names from the code examples in the linked tutorial).
just keep in mind to call reader.readAsText to make the file contents available to javascript.
processing uploaded files with the perl module CGI.pm as part of a cgi script.
drop a comment if you need more support to get your job done. the explanations and the sample code in the cited docs are actually pretty good.
Also have a look at the answer to the question Quentin ref'ed if you haven't yet done so. regards

Related

How can I modify a .JSON file stored on server from client side?

I'm working on a webapp that is currently running on a server, where there are also some .JSON files, which I would like the user to be able to type some information and press enter where it will be stored to the .JSON file.
The webapp is written in HTML, CSS, and vanilla Javascript, there aren't any external libraries being used.
I am already pulling information from the .JSON files to be used in the app using the GET function, and I know I can't directly modify server side files with javascript unless I'm running a Node.js server (which isn't currently an option).
So I believe my only option is to use a server side language such as PHP (which I nothing about), to modify the file. My question is, how can I do this relatively simply? Possibly when a JS function is run to push the change to the file.
Can anyone give me a sliver of example code, or point me in the direction of some simple documentation or tutorial on how to do this, I'm not very adept at server side programming at all, and as this is a simple project just for me, I don't want to dive deep into PHP at the moment.
Thank you in advanced!
So I believe my only option is to use a server side language such as PHP (which I nothing about), to modify the file.
Since you already know JavaScript, I'd revisit why you feel you can't use Node.js server-side. In any case, if you're going ahead with PHP...
To write files, file_put_contents().
To encode JSON, json_encode().
Note that there are other servers out there. You don't have to write your own stuff in PHP. If you don't need any checking on what's being sent, you can probably even modify your web server's config to accept a PUT.

Writing data to a local file using HTML5 and Javascript in Chrome

I have created an interactive quiz using html and javascript which will be run on a touchscreen at an event and I need to write the results to a local csv file (so no internet connection). It needs to write to an already existing file, so it cannot be done where the data is stored locally and a download link is generated through the browser.
How would I go about doing this? All methods I have found are either unreliable or no longer supported. The browser I am using is Chrome, so it does not need to be cross-browser compatible.
Can anybody help or point me in the right direction please?
Install a web server.
Point the browser at http://localhost.
Send the data to the server using Ajax or a form submission.
Process the data (including storing it in a file) using the server side language of your choice.
When javascript is used only in the client-side cannot write data as you want.
Follow the #Quentin recommendation about install some web server, as apache using php for instance(It is pretty straight forward!). I also recommend you to create restFull methods to do it with jquery calls from the client side, it is easy to find many examples in the internet and quicky...
If you want something more easy you could work with html post using forms in php, the most easy way to do it.

Is it possible to make an AJAX call to JavaScript instead of ASP?

I am making somewhat of a content delivery network (CDN), but I want to have a jQuery AJAX call to a JavaScript file instead of an ASP file. The JavaScript file would then handle the input and then return a response. I don't want to use ASP, because I don't want to set up a whole server, especially because I am on a Mac. If this isn't possible that's fine, I'm just wondering.
(I can not provide any code, because I don't have enough that it would be useful.)
If you want to use AJAX to retrieve a JavaScript file, that's kinda possible. You'll just need to dynamically add a <script> tag to the page (with the URL of the JS file in question) and the browser will take care of the rest. Don't forget you can add an onload handler to execute some code when the file's loaded.
If, however, you wanted to run JavaScript on your server, there's the excellent NodeJS for that. You will need to have another server running in this case.
Either way, there's not really a way to have code running outside of the browser and not have a separate server. Hope this helps, comment with any further questions.
I wonder if you're just trying to lazy load a JavaScript file from a URL, but that isn't your question, so I'll ignore that.
You can do just about anything, but that doesn't mean you should. You can't directly make an Ajax call to JavaScript. That's just a meaningless statement... The semantics of what you actually said would require an Ajax call to a server that has the ability to execute JavaScript code like a browser would, which can be done with a headless browser like PhantomJS. So, you would have to install PhantomJS on your server, Ajax to the server, have the server use PhantomJS to process some JavaScript code, then have the server respond to the Ajax call with whatever result you come up with.
I have to reiterate: You should not do this ^.
It would be like traveling the world to get to your neighbor's house. Anything that is possible within all of that mess, you could have just done it in the JavaScript code on the page or on the server in the server-side language.

Can I save a file which will be generated from php code on server side using only javascript/C/C++ and no browser opened?

What is the best way to save a file from internet on javascript and/or C and/or even C++?
I saw this same question for C# and Java, but nothing to this three languages, so here is the question.
Hey, not so easy. The url point to some http://xx.xxxx.com/p.php?pid=staticetctectc....
I guess is php code which produce a nice gif in my browser. I just want to save this gif. Without opening browser. It is possible to do with javascript/C/C++?
Most near answer I found is this.
Thanks in any advice.
You can easily do this with JavaScript using Node.js. Here is a link to an example: http://www.hacksparrow.com/using-node-js-to-download-files.html
You could also do it from the command line using wget or curl. They are both available on pretty much every platform you can imagine.
For C++, you could do it like in Downloading File in Qt From URL. If you do not want to depend on Qt, libcurl is also an option.
For JavaScript, the method described in File Download Using JavaScript should work.

Extracting jpeg EXIF information using jQuery

I'm trying to extract EXIF information from images using jQuery.
I've tried using a script I found here but I can't get it to read exif data from externally hosted image files. I've posted a simple example page here. The code is tiny so you can just view-source to see what I tried.
Does anyone know either what I'm doing wrong or if there there is another way to do it?
-Matt
The plugin works off requesting the image again via a binary XmlHttpRequest, something you just can't do on a remote domain due to the same-origin policy...the browser does a pretty good job of (by default) separating which domains interact with others, this is one of those cases (for security reasons).
Sorry the answer sucks, but I don't think you're not going to be able to do this on a remote image, on a domain you don't control.
You'll either have to proxy the images on the same hostname or get the external host to send appropriate access control headers to allow you to request data on their site through JavaScript.
I always wondered how people seem to think anything could be done with javascript.
Given a remote image, isn't it obvious you could(should) download the image on the server and do the special processing there?
Also, what makes you think jQuery can do things javascript doesn't?
What you are looking for is, at it's best, something that needs to be supported in plain javascript.
Really, parsing a remote binary file locally? Sure it's a nice hack, but why not settle for the real/pro solution, make the server parse the image?
Rant aside, the JS lib cannot work on remote files, and this can't be ever done with plain JS.
You should be downloading the file on your server and parsing it from there.

Categories