Is it possible to upload a file to a certain page using PhantomJS without submitting manually the form? I think something is possible using the Content-Type: multipart/form-data.
The example on https://github.com/ariya/phantomjs/blob/master/examples/imagebin.js is working fine, but I want to send directly the file in the POST request without interacting with any element.
Any suggestion?
Thank you very much
File uploads can be done over AJAX (as of xhr2 - if you need to support older browsers, use something like jQuery-File-Upload; there is a good tutorial here on using it just to do the upload part, and not using Blueimp's UI.) That is related to the "without manually submitting the form" part of your question. You will still need to interact with a file upload element to choose the file, and that is where you use page.uploadFile().
To do it solely from JavaScript, without "interacting with any element on the page", you could use page.evaluate() to run some custom JavaScript. It could then use the File Reader API to find the file on local disk, store it in a blob, then upload that blob over AJAX. The WebKit in PhantomJS (roughly equivalent to Chrome 13) should work, as apparently the FileReader API has been there since Chrome 6.
(BTW, if all you wanted to do is upload a file to a server using a headless script, PhantomJS is overkill, and you could use curl. But I'm assuming you want to use PhantomJS for some other reason!)
The solution I've used at the end is a mix of NodeJS and PhantomJS, in case I need to upload something I fork a process and upload the file(s) using the NodeJS "request" module and then send back to PhantomJS the output of the page, which need to be manually processed.
Related
I need to solve a problem where I parse a csv file via a Python script and generate a new file. I've written the script, but the tricky part is that the file to be handled needs to be uploaded via a browser and the result downloaded likewise. The idea is that it shouldn't require a webserver, it needs to be something like a folder, where you open index.html, upload a file and get a download prompt for the result and I'm looking for the most lightweight solution (I am aware I could do this with Django or Flask, but a whole framework for a tiny tool seems overkill).
This may be a bit confusing, so I'll try to clarify the first hurdle:
How do I call a python script from a webpage without setting up any webservices (if possible) or what would be the quickest way to achieve this. The webpage isn't really a webpage, it will never be deployed on a webserver, it's just an interface for the script.
For the upload part you can use requests :
How to send a file from a python script
For the download part, do you know the url without having to ask anything to the remote server ? If yes, you can still use requests.
I need to access a local file on the client side of a Django project and read an xml file from the client's local disk. Like C:\\test.xml
I am doing this in a single html and script file and using Chrome --allow-file-access to get permission for this access and it works but when I move this code into my Django project and use this jquery script in my html templates, it does not work and shows cross origin request ... error.
Please help me. Why is this happening and what is the solution?
Thanks.
One viable option that would work and not set out security alarms all over the place, would be to use file form field on your page and ask an end user to give that file to you.
Then, you can use HTML5 File API and do whatever you need in javascript or send it to your server.
I have made a project whose structure is like this
When I run my index.html from firefox, it is working fine.
But I open it with my chrome it is giving CORS error.
Now my problem is that chrome doesnot support file:// protocol ajax request & as I have distribute to my project to others I don't want other to run on firefox only.
Internally I am using ajax call in project, to load the resources. Can Somebody suggest how to bypass that ajax call to load resources?? Is there some solution or any third party js which can help me.
Note: Please don't suggest to use XAMPP, apache etc where I can put my project and run as localhost for chrome as I don't want user to force to download these to run my project. Please give other useful solution where I can do some change in code & it works for everyone.
Here are the links from which you could understand my problem better.
Ajax in Jquery does not work from local file
AJAX code do not run locally
AJAX request using jQuery does not work
Embed the data directly into the JavaScript or HTML and read it from there.
The data isn't going to be changing based on user input or the contents of a database, so having it in a separate "http" resource doesn't bring huge benefits.
If you want to store the data in XML to make it easier to edit in your development environment, then write a build tool to bundle it up into an embedded format before distributing.
I am trying to process an XML file using Javascript.
xhttp.open("GET","exportproject.xml",false);
What I want to do is, let the user specify the file (instead of hard-coding it to exportproject.xml) using file uploader and then process the same using Javascript instead of sending it to the server.
Is it possible?
You may want to take a look at the HTML5 FileReader API - http://www.html5rocks.com/en/tutorials/file/dndfiles/
If you don't mind a solution that requires a modern browser (basically, ie 9+) you can use the html5 file API with a basic <input type="file">.
Take a look at this link, there are a number of excellent examples to get you started.
Javascript cannot read a file from the client machine (where the browser runs). That would be a security violation. You will have to submit the file to server and process it.
Is it possible to upload file in facebook fbml (not iframe) using Ajax ? not a complicated one but just use simple input type="file" tag wrap in a form and post the from using facebook builtin ajax. If can, then how to accept it in server ? I'm using rails facebooker as my backend and paperclip plugin as my file upload system.
Thanx
When I was looking for AJAX uploads, the best I could find was only using iFrames. From this, I assume it's not possible?
client can=server can.
There is no easy way to do so in a fbml app. However what you can do is that you should create a normal file uploading script (non fbml) and submit your request to that script.
This script uploads the file and goes back to the original fbml page.
I have done that in following app:
apps.facebook.com/theaffinity
Shouldn't it be possible to do something like in Gmail (i.e. AJAX file uploader with loading bar and not having to refresh the page) on a FBML page? Probably with a hidden fb:iframe that would post directly to your own server (not facebook's)..
I'm afraid it would probably take me days, but if anyone has figured it out, please post back :)