By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

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

omerbutt
100+
P: 638
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 `autoProcessFiles=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.addFile()` 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

✓ answered by omerbutt

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.prototype.initfunction in there you will see a check

if (this.clickableElements.length) {

inside this check dropzone creates & configures the hidden file input and then attaches that input element to the body document.body.appendChild(_this.hiddenFileInput); 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.hiddenFileInput.addEventListener("change", function() {

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

files = _this.hiddenFileInput.files;

if you try to log it in console console.log(files) 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.addFile(). 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

Share this Question
Share on Google+
1 Reply


omerbutt
100+
P: 638
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.prototype.initfunction in there you will see a check

if (this.clickableElements.length) {

inside this check dropzone creates & configures the hidden file input and then attaches that input element to the body document.body.appendChild(_this.hiddenFileInput); 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.hiddenFileInput.addEventListener("change", function() {

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

files = _this.hiddenFileInput.files;

if you try to log it in console console.log(files) 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.addFile(). 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

Post your reply

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