Change image file name to unique using dropzone.js - javascript

I'm trying to make an image upload website using Dropzone.js.
I need to change the image to a unique name but I can only change the display name or the name that the image saves.
This the code that I got now:
Dropzone.options.fileupload = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 10, // MB
init: function() {
this.on('processingfile', function(file) {
file.name = 'test.jpg';
});
}
};

There is no clear way to do this.
Uploading multiple files with the same file.name using dropzone.js
Use processing event: (not working)
myDropone.on('processing', function(file) {
file.name = 'justin-bieber.jpg';
});

Related

Direct File Upload Javascript

How is it possible to upload a file directly without clicking file upload button(I want to click Add Widget Button which should give file upload dialog box)
I have a button declared as follows:
<button class="add-button" style="float:top;">Add Widget</button>
On clicking the button the following function is invoked
$(".add-button").on("click", function() {
// get selected color value
var color = $color_picker.val();
// build the widget, including a class for the selected color value
var $widget = $('<li>', {
'class': 'color_' + color
})
.append($('<button>', {
'class': 'delete-button',
'text':'-'
}))
.append($('<img src="abc.png" height="60px" width="60px">'));
// add widget to the grid
gridster.add_widget($widget, 1, 1);
});
But I first want a upload box to appear where in user can upload the image as soon as the button is clicked then the above code should get executed
I did something like this
$(".add-button").on("click", function() {
var x = document.createElement("INPUT");
x.setAttribute("type", "file");
x.setAttribute("onclick","previewFile()");
// get selected color value
var color = $color_picker.val();
// build the widget, including a class for the selected color value
var $widget = $('<li>', {
'class': 'color_' + color
})
.append($('<button>', {
'class': 'delete-button',
'text':'-'
}))
.append($('<img src="abc.png" height="60px" width="60px">'));
// add widget to the grid
gridster.add_widget($widget, 1, 1);
});
But this does not brings any dialog box where user can upload the image
This uploaded image I want to then use in the place of
.append($('uploaded image'));
Preview File Function (This also needs to be modified)
function previewFile() {
var preview = document.createElement('img');
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader(); //API for reading file stored on user computer
reader.addEventListener("load", function () { //"load" :This eventlisterner "listens" loading of file. Once it is loaded function is triggered
preview.src = reader.result;
});
if (file) {
reader.readAsDataURL(file); // helps in reading the content of "file"
}
document.body.appendChild(preview);
}
My aim is that preview file function should return an image which I can put in
.append($('image from preview file'));
A version of code is at Fiddle
The way to do this is to have some hidden input with file type somewhere on the dom. You might be able to programatically put it there, but really no point in that. Once the add widget button is clicked, you can simulate a click for the hidden input. This will initiate a prompt to pick a file. Then what you want to do is, wait until the file has been "picked" by the user. This is done via the onchange event. Within that, you can grab the file, read it, and then when it's done you can have a callback via the onload method. I put up a working example here. I imagine you wanted to have the file picked as the image set on the src, so I did that as well.
hidden input
<input id="test" type="file" style="position: absolute; top: -10; left: -10; height: 0; width: 0;" />
button click function (this is what will wait until the file has been chosen), the call back method is used on the onload event of the filereader.
$(".add-button").on("click", function() {
$('#test').click();
$('#test').on('change', function(e) {
var test = document.getElementById('test');
if (!test) {
alert("Um, couldn't find the fileinput element.");
}
else if (!test.files) {
alert("This browser doesn't seem to support the `files` property of file inputs.");
}
else if (!test.files[0]) {
alert("Please select a file before clicking 'Load'");
}
else {
file = test.files[0];
console.log(file);
fr = new FileReader();
fr.readAsDataURL(file);
fr.onload = function() {
var data = fr.result; // data <-- in this var you have the file data in Base64 format
callbackAddButton(data);
test.value = ''; //here we are resetting the file input's files
$('#test').replaceWith($('#test').clone()); //here we are resetting the input, if we clone the input with the files then it wont trigger the onchange event if you upload same image. So we need to make sure we have these 2 calls.
};
}
})
});
And finally, the callback method. Which is simply exactly what you had before but now only gets called once the file has been done (done as in, you read it via filereader and have access to contents if needed). The only difference here is now you have a base64 representation of the image the user uploaded. Which I am setting to the new image widget you created.
function callbackAddButton(file) {
// get selected color value
var color = $color_picker.val();
// build the widget, including a class for the selected color value
var $widget = $('<li>', {
'class': 'color_' + color
})
.append($('<button>', {
'class': 'delete-button',
'text':'-'
}))
.append($(`<img src="${file}" height="60px" width="60px">`));
// add widget to the grid
gridster.add_widget($widget, 1, 1);
}
EDIT
Once you're done with the input file element, it's good practice to now clear it because you dont need the file anymore (from the input at least, since you have a base64 representation). Just append a $('#test').replaceWith($('#test').clone()) after the you make the callback call.

Saving an image to app properties

Hi I'm trying to save an image to the apps properties as part of a JSON array but when i try to display it, the imageview appears blank. The image is grabbed from the phone's gallery when the Upload_pic button is clicked. Code below:
Upload_pic.addEventListener('click', function (e){
Upload_pic.setImage('/images/uploadPhoto2.png');
Titanium.Media.openPhotoGallery({
success: function(e){
console.log("typeof e.media -------- " + e.media);
ImageView=Ti.UI.createImageView({
image:e.media
}).toImage();
},
error: function(e){
alert("Error accessing photos");
Upload_pic.setImage('/images/uploadPhoto1.png');
},
cancel: function(e){
Upload_pic.setImage('/images/uploadPhoto1.png');
},
alowEditing:true,
mediaType:[Titanium.Media.MEDIA_TYPE_PHOTO]
});
});
and then I'm saving the image on this submit_btn click:
Submit_btn.addEventListener('click', function (e){
var filename = ID+"-ea.jpg";
bgImage = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
bgImage.write(ImageView);
if (bgImage.exists()) //File ever does not exist.
{
alert('file '+filename+' exists!!!');
// I am getting this alert
}
else
{
alert('file '+filename+' IS NOT exist(');
}
MyRecipe_info= {name: title_textField.value, ID:ID, Ingredients: ingredients_textField.value, numSteps:numSteps_textField.value, pic:bgImage, steps:numStepsArray};
Ti.App.Properties.setBool('MyRecipeAdded', true);
MyRecipes_data.push(MyRecipe_info);
Ti.App.Properties.setString('MyRecipesData', JSON.stringify(MyRecipes_data));
alert("Recipe added successfully!");
});
And then i am displaying it here:
MyRecipes_data=JSON.parse(Ti.App.Properties.getString('MyRecipesData'));
for(i=0;i<MyRecipes_data.length;i++){
var Pic = Titanium.UI.createImageView({
left: '5dp',
top:'20dp',
right:'15dp',
height: Ti.UI.SIZE,
bottom: '5dp',
image:MyRecipes_data[i].pic
});
}
Currently working on iOS, but code will need to work on Android in future. Using Titanium SDK 3.3.0.201407100905 and Titanium Classic
Problem was I needed to store the bgImage.nativePath
to store a image in property is bad habit as it takes much memory specially for android so i would suggest to store only image path in that array
or even if possible you should use database to store data instead of using Ti.Property.
Hope you get it.

Dropzone.js : add already stored files not triggering events

I'm using dropzone.js and node.js to add an easy drag-and-drop interface for a user to upload up to 10 photos to server.
Here is my init function:
init: function(){
var photos = $('#property-photo').data('photos').split(',');
for (var i = 0; i < photos.length; i++) {
var photo = "/photo/" + photos[i];
var mockFile= {type: "image/*", name: 'photo', size: 1500};
this.addFile.call(this, mockFile);
this.options.thumbnail.call(this, mockFile, photo);
}
}
The thumbnail is shown but when i call listingDropzone.processQueue(); - it throws an error:
Failed to execute 'append' on 'FormData': No function was found that
matched the signature provided.
Only if i remove the image and add a new one it works.
Any ideas?
Dropzone can't upload a mock file. Mock file was already uploaded.
You can see this issue: https://github.com/enyo/dropzone/issues/418

Download images on client to zip

Okey, so what I want to do is download multiple images stored in an JavaScript array in AngularJS. I have not found any way of doing this in the form of "pure" images like .png .jpg etc (although if anyone knows how to do this please let me know). So I have turned to trying to zip the images with the help of jszip.js according to the description here : http://viralpatel.net/blogs/create-zip-file-javascript/
<li ng-click="download()"><a>Save to harddrive</a></li>
So this is the code called when the user clicks "download":
photodice.controller('ProjectController', function($scope, imageFactory) {
$scope.images = imageFactory.getImages();
$scope.download = function() {
var zip = new JSZip();
for (var x in $scope.images) {
zip.folder("images").add("image" + x + ".png", $scope.images[x], {base64: true});
}
var content = zip.generate();
console.log("content = "+ content);
location.href="data:application/zip;base64," + content;
}
});
The problems occurs when the last line of code is executed, the browser crashes... I do not really understand how filedownloads work... Any suggestion or tips on what I should read up on would be greatly appreciated!
Update:
I tried using Downloadify to solve my problem... and added some code:
$scope.download = function() {
var zip = new JSZip();
for (var x in $scope.images) {
zip.folder("images").add("image" + x + ".jpg", $scope.images[x], {base64: true});
}
Downloadify.create('downloadify',{
filename: function(){
return "Images.zip";
},
data: function(){
return zip.generate();
},
onComplete: function(){
alert('Your File Has Been Saved!');
},
onCancel: function(){
alert('You have cancelled the saving of this file.');
},
onError: function(){
alert('You must put something in the File Contents or there will be nothing to save!');
},
transparent: false,
swf: 'downloadify.swf',
downloadImage: 'img/download.png',
width: 100,
height: 30,
transparent: true,
append: false,
dataType: 'base64'
});
And now I can save the zip file:)
HOWEVER I still have a problem... the files in the zip are corrupt...
Normal non corrupt images look like this:
"data:image/jpeg;base64,iVBORw0KGgoAhE... ...c8+ocAAAAORK5CYII="
If I upload "currupt" images to my site agian and check the scope of the images that are corrupt the data looks like this:
"data:image/jpeg;base64,dataimage/jpegbase64iVBORw0KA... ...RNgAK5CYII="
Alternatively I remove the {base64: true}:
"data:image/jpeg;base64,ZGF0YTppbWF... ...WT1JLNUNZSUk9" (no "=" at the end)
What can I do about this?
I'm putting the answer here for future reference (because it was buried in the comments).
The issue comes from $scope.images[x] which contains a string, "data:image/jpeg;base64,iVBORw0KGgoAhE... ...c8+ocAAAAORK5CYII=". JSZip interprets the data:image/jpeg;base64, part as base64 content and generates corrupted images. To remove this part, you can do the following :
var base64 = $scope.images[x];
var index = base64.indexOf(",");
if (index !== -1) {
base64 = base64.substring(index + 1, base64.length);
}
zip.folder("images").file("image" + x + ".jpg", base64, {base64: true});
Also, the add() method you use has been deleted in 2011 so you may want to update JSZip ! (but that may need some work : only folder() and remove() haven't changed between your version and the latest).

Dropzonejs mulitple zones with file preview

Hoping that someone will be able to tell me where Im going wrong here .
Im using Dropzonejs to allow people to drop files for storage online, I have 8 zones on one page, each used for a different category, when someone drops a file they are saved with additional data depending on which zone they were dropped in for later retrieval .. this all works fine.
I then wanted each zone to show the thumbnails for each of the files on the server under its category on page refresh, this is where I get problems, using the follwing code I get all my files being shown in the bottom zone, and the others are empty. Its obviously a problem with the code that fetches current files and makes the thumbnails, but Im lost as to why.
If I only have one instance on a page it all works fine.
Heres the code I use to setup and instantiate each dropzone instance.
Dropzone.options.dzdiag = {
url: "upload.php",
previewsContainer: "#dzdiag",
init: function() {
thisDropzone = this;
filetype = "diagnostic";
var propid = <?php echo $id; ?>; // set value to current property id
$.get('upload.php?propid='+propid+'&filetype='+filetype, function(data) { // get exisitng files and thumbnail
$.each(data, function(key,value){
var mockFile = { name: value.name, size: value.size, fileid: value.fileid, };
thisDropzone.options.addedfile.call(thisDropzone, mockFile);
var pattern = /\.(gif|jpg|jpeg|tiff|png)$/i;
if( pattern.test(value.name)){
thisDropzone.options.thumbnail.call(thisDropzone, mockFile, "uploads/"+value.name);
}
});
});
thisDropzone.on("sending", function(file, xhr, formData) {
formData.append("propid", propid);
formData.append("filetype", filetype);
});
thisDropzone.on("success", function(file, serverack) {
file.fileid = serverack;
});
thisDropzone.on("removedfile", function(file) {
var delid = file.fileid;
$.post('upload.php?delid='+delid);
});
}
};
$("#dzdiag").dropzone();
Simply changing the previewsContainer (on not specifying it) for each zone should get you what you want.

Categories