473,549 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dropzone.js : add file to queue without File Browser dialouge

omerbutt
638 Contributor
i am using dropzone.js in my project , what i want to do is add file manually to queue without opening the file browser dialogue, the dropzone is intialized on a page and i am trying to run the following script to add the file
Expand|Select|Wrap|Line Numbers
  1. Dropzone.autoDiscover=false;
  2.     var myZone=Dropzone.forElement('.imageDropzone');
  3.     var fileList=$("input[accept='image/jpg,image/gif,image/png,image/jpeg']")[0].files;
  4.     fileList.name="imageUploadTestJPG.jpg";
  5.     fileList.type="image/jpeg";
  6.     fileList.size=30170,
  7.     fileList.path="http://mysite/img/imageUploadTestJPG.jpg";
  8.     fileList.mozFullPath="http://mysite/img/imageUploadTestJPG.jpg";
  9.     fileList.accept="image/jpg,image/gif,image/png,image/jpeg";
  10.  
  11.     console.log(fileList);
  12.     myZone.addFile(fileList);
  13.  
**why i am doing this?**

**1**. I am working with selenium php-webdriver by facebook and i need to test the upload functionality,

**2.**
The file browser dialogue that is opened after clicking on a file type input is OS dependent, different in different OS, and i cant shift control to that window, so i wanted to skip this process of opening the file dialogue by clicking on it and want to add the file manually by javascript / jquery and keeping `autoProcessFile s=true` so that as soon the file is added the upload process starts, bu i am unable to solve it.

when i try to call `Dropzone.addFi le()` i receive the following
TypeError: Argument 2 of FormData.append does not implement interface Blob


I event tried another way i.e

**1**. Add the file path to the file input on which the dropzone was initialized because dropzone binds an `change eventlistener` with all the `file inputs` which are initialized with dropzone and as soon the path to file is provided the `change event listener` triggers and starts uploading the file, but trying to modify the value for file input with dropzone initialized raised security exception.

**2**. Moreover the `<input type=file>` is made hidden by the `dropzone.js` script when initialized and php-webdriver does not allow to interact with hidden elements so i am stuck with this, any help or guidance would be appreciated.

Regards,

Omer Aslam
Jan 5 '15 #1
1 15431
omerbutt
638 Contributor
Done,

The problem was with the format of FileList object that was provided to the myZone.addFile( ).If you open dropzone.js file and go to Dropzone.protot ype.initfunctio n in there you will see a check

if (this.clickable Elements.length ) {

inside this check dropzone creates & configures the hidden file input and then attaches that input element to the body document.body.a ppendChild(_thi s.hiddenFileInp ut); and right after this line dropzone adds the change eventlistener to file type input that was created which fires as soon as we provide the file via browse file window.

return _this.hiddenFil eInput.addEvent Listener("chang e", function() {

When we provide the file it fires and creates the FileList object see

files = _this.hiddenFil eInput.files;

if you try to log it in console console.log(fil es) you will see a FileList that is created FileList { 0=File, length=1, item=item(), more...} on clicking this object inside firebug you will see detail below

Expand|Select|Wrap|Line Numbers
  1. 0          File { size=21789, type="image/png", name="1-7-2013 6-19-44 PM.png", more...}
  2. length      1
  3. __proto__   FileListPrototype { item=item(), @@iterator=@@iterator()}
now the way i was creating the filelist object the result was following

Expand|Select|Wrap|Line Numbers
  1. _removeLink -----  a.dz-remove javascrip...defined; 
  2. accept      -----  "image/jpg,image/gif,image/png,image/jpeg"   
  3. accepted    -----  true 
  4. mozFullPath -----  "http://mysite/img/imageUploadTestJPG.jpg"   
  5. name        -----  "imageUploadTestJPG.jpg" 
  6. path        -----  "http://mysite/img/imageUploadTestJPG.jpg"   
  7. previewElement --   div.dz-preview  
  8. previewTemplate --- div.dz-preview  
  9. processing    ----- true    
  10. size                30170   
  11. status        ----- "uploading"
  12. type                "image/jpeg"    
  13. upload        -----  Object { progress=0, total=30170, bytesSent=0}
  14. xhr            XMLHttpRequest { readyState=1, timeout=0, withCredentials=false, more...}
  15. length             0
  16. __proto__          FileListPrototype { item=item(), @@iterator=@@iterator()}
  17.  
Notice the index 0 on the first detail which contains the detail of the file whereas the second one which is the result of my custom built FileList object has all the details of the file on the main rather than inside the index 0.

So to create an object same like that i had to first obtain the blob via sending the xmlHttpRequest request to the image and specifying the response type of arraybuffer and then Obtain a blob URL for the image data and then assign that response to the file object and assign that to the input.file and calling the Dropzone.addFil e(). the complete description for the process is below and you can upload file without opening the file browse window and use dropzone.js with selenium

Expand|Select|Wrap|Line Numbers
  1. // Simulate a call to  service that can
  2. // return an image as an ArrayBuffer.
  3. var xhr = new XMLHttpRequest();
  4.  
  5. // Use JSFiddle logo as a sample image to avoid complicating
  6. // this example with cross-domain issues.
  7. xhr.open( "GET", "http://localhost/path/to/my/image.jpg", true );
  8.  
  9. // Ask for the result as an ArrayBuffer.
  10. xhr.responseType = "arraybuffer";
  11.  
  12. xhr.onload = function( e ) {
  13.     // Obtain a blob: URL for the image data.
  14.     var arrayBufferView = new Uint8Array( this.response );
  15.     var blob = new Blob( [ arrayBufferView ], { type: "image/jpeg" } );
  16.     var urlCreator = window.URL || window.webkitURL;
  17.     var imageUrl = urlCreator.createObjectURL( blob );
  18.  
  19.     var parts = [blob, new ArrayBuffer()];
  20.  
  21.     file = new File(parts, "imageUploadTestFile", {
  22.         lastModified: new Date(0), // optional - default = now
  23.         type: "image/jpeg" 
  24.     });
  25.  
  26.     $("input[accept=\'image/jpg,image/gif,image/png,image/jpeg\']").files = [file];
  27.     myzone = Dropzone.forElement(".imageDropzone");
  28.     myzone.addFile(file);
  29. };
  30. xhr.send();
Hope it helps others who are searching to do the same
regards,
Omer Aslam
Jan 8 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
5438
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. I want to develop a webpage where people can send attachments that are stored on their local PC.
2
7099
by: Lin Ma | last post by:
Greetings, Is it possbile to check a file exist without using Server.CreateObject("Scripting.FileSystemObject") in asp page?? The reason is our hosting company turn that function off for security reason. Here is my original code: <%
2
1533
by: sajithamol | last post by:
Is there any pre-defined java script function for invoking the file menu of a browser directly, just like window.print()? Is there an option to invoke the view source tab directly using a function?
2
3902
by: sajin | last post by:
Hi All, I am using .Net 1.1 to create my windows form application. I need to show a flash file (.swf) in the browser. When i load this thought 'Navigate' method, it is giving some script error. Plz Help Regards -Sajin
38
5047
by: ted | last post by:
I have an old link that was widely distributed. I would now like to put a link on that old page that will go to a new page without displaying anything.
2
3533
by: Bart Kastermans | last post by:
I have written a little program that takes as input a text file, converts it to a list with appropriate html coding (making it into a nice table). Finally I want to upload this list as a textfile using ftp. If homeworkhtml contains the list of lines; e.g. homeworkhtml = ["<table>", "<tr>", "<td>", "test", "</td>" ..... I want to call:
9
3639
by: Denis | last post by:
Hello, Maybe somebody already solved this task. I need to load JS asynchronously without blocking browser. This means that I do not need to block browser to load/render during loading script. The simple construction to do this: jsJSNode = document.createElement(“script”); jsJSNode.type = “text/javascript”; jsJSNode.src = “http://...
1
2019
jovit1188
by: jovit1188 | last post by:
Help me with my dilemma on how to save (.mdb) file wihtout using savedialog browser.. i know how to save into text file but i searched the internet about saving a database file (.mdb) to a path but none fits my requirement.. thanks..i'm sorry for frequently asking questions it's just that i am new to C# and SQL.. Thanks for all the help..
0
1039
by: pmatt77 | last post by:
I'm working on two windows 7 computers, with IE9. when attempting to open a tiff file in the web browser its displayed as a aspx file. One the other computer it opens as a tiff file within the browser and launches an image viewer program.
0
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7457
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7817
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.