Upload file with PhantomJS to a Dropzone - javascript

My code for uploading a file is this:
var uploadEl = document.getElementById("upload"); // div
page.upload(uploadEl, 'C:/temp/1.JPG');
This actually hangs, so my question is, what is the proper way to upload a file to a dropzone with PhantomJS?
The way a user manually upload a file is like a user clicks the upload div browser opens a native File upload select, the user selects the file and hits the Open button and the website triggers the upload via ajax.

The correct way to upload files in PhantomJS is this:
var page = require('webpage').create();
page.uploadFile('input[name=image]', '/path/to/some/photo.jpg');
It should also work for Dropzone, because uploadFile method behaves as if a user picked a file with a file dialog:
This function is used to automate the upload of a file, which is usually handled with a file dialog in a traditional browser. Since there is no dialog in this headless mode, such an upload mechanism is handled via this special function instead.
Source: http://phantomjs.org/api/webpage/method/upload-file.html

Related

How do I write a function to autodownload pdf files from a link without navigating to another webpage either in java script or html

I am trying to implement the auto download feature in my web application where when clicked on a button a pdf file should download. I have the link but I am not able to download the file . I have used http get also and ionic file transfer plugin but neither of them are working. Can anyone help me write a code that does this task?
You can use the new download attribute for elements (new in HTML5). The following is from "https://www.w3schools.com/tags/att_a_download.asp"
"Specify a value for the download attribute, which will be the new filename of the downloaded file ("myFile.pdf" instead of "sampleFile.pdf"):"
Download PDF
Download PDF

How to programmatically upload file in node-webkit / nw.js

I am using node-webkit/nw.js as an automation tool to test my web application.
My application has a record mode to capture all click events and keyboard events.
All the captured events are logged into a file and while in play mode, the logged events are run from clean state.
However, since file uploads open up a File dialog, I cannot emulate a file selection. Since am using node webkit, I can programmatically read a file from the user system and upload it as blob using FormData and Blob . However, based on the file upload, there are events that have to be triggered, and after the upload is complete other fields have to populated say with an image.
Is there a way I can attach a file in the form data programmatically?
I can understand it is not possible in a normal browser since it would be a security issue. In a node webkit environment am anyways able to access and upload user file in the background. So, will it be possible to modify the input file value or by some other means add a blob inside the Form and upload the file?
It seems the steps involved is elaborated in the github page of nw.js but not on docs.
Steps to get reference of form node and input node.
//Detect if button clicked is inside a form.
//In my case all attachments use same component. So I know the no. of levels to move up
if(activeElm.parentNode.parentNode.parentNode.tagName=="FORM"){
//Navigate to input elm and save it as new activeElm.
//Navigation will differ based on your page.
activeElm = activeElm.parentNode.parentNode.parentNode.getElementsByTagName('input')[0];
}
Steps to emulate file attachment.
var f = new File('/path/to/file', 'name');
var files = new FileList();
files.append(f);
elm.files = files;

Uploading file using ajax (without FORM)

I have some files in the local and know the path of those files.
I'm trying to upload the file using AJAX.
How to upload the file using AJAX without using FORM.
enter code here
NOTE: I've the FileReader object of the file.
Please share if u have any idea.
Thanks
You cannot upload a file from the local computer unless triggered by the user (the user should select the file), you can upload a file with jQuery:
https://github.com/blueimp/jQuery-File-Upload
https://github.com/hayageek/jquery-upload-file

Showing a bootbox.js dialog to make a selection during dropzone.js form action while sending to node.js server

I have a node.js/express site which has an upload dropbox which use dropzone.js to do drag and drop uploads. My dropzone inside my jade template looks like this currently:
div(id="dropzone")
form(method="POST" action="/upload" class="dropzone" id="my-dropzone")
div(class="dz-message")
span Drop your zipped AgentCubes project here to upload it
This currently works fine, but in the case where the user uploads the same file again I would like to provide the option to overwrite the old file or generate a new name for the duplicate upload. This would not work with my current scheme which simply sends the file /upload which is an express endpoint I have created which currently handles the upload.
It is also complicated by the fact that I would need to post to the server to decide if the file is a duplciate upload, then show the bootbox.js dialog in the client and then finally send the upload data to the server. I have looked through the dropzone.js documentation but I can't seem to find anyway to accomplish this. How can this be done?

How to save innerhtml to text file, html file on some folder using java script?

I am using .aspx page, i want to save some data on button click, which i extracted using function
function save() {
var t1 = document.getElementById('test').innerHTML;
alert(t1);
}
to .text file, .html file some folder on desktop.
the folder should appear, where i can save the file with any extension of .text or .html.
Javascript in the browser has no file i/o capabilities. The best you can do is popup a window with just the text you want to save and then save using the browser or send the text to the server and have it serve up the appropriate download mime-type as a new page.
You can't with javascript. You'll have to send the data file server side. See C# Asp.net write file to client.

Categories

Resources