JQuery Dropzone options not working issue - javascript

I have created a dropzone file upload.My html code is
<form action="upload.php" class="dropzone" id="myDropzone"></form>
And options are
$(document).ready(function() {
Dropzone.options.myDropzone = {
dictDefaultMessage: "Select photos",
paramName: "file",
maxFilesize: 0.5,
clickable: false,
renameFile: function (file) {
return file.name = new Date().getTime() + '_' + file.name;
}
};
});
But the options are not working.I am new to dropzone.How can i do this

https://codesandbox.io/s/peaceful-varahamihira-cci6y
It's easier to create an instance of Dropzone than trying to use "auto discover" feature.
$(document).ready(function() {
// Prevent Dropzone from auto discovering this element:
Dropzone.options.myDropzone = false;
// This is useful when you want to create the
// Dropzone programmatically later
// Disable auto discover for all elements:
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("#myDropzone", {
url: "upload.php",
dictDefaultMessage: "Select photos",
paramName: "file",
maxFilesize: 0.5,
clickable: false,
renameFile: function(file) {
return new Date().getTime() + "_" + file.name;
}
});
myDropzone.on("complete", function(file) {
console.log(file);
});
});

Related

send data with dropzone

I work with dropzone:
<form action="#" class="dropzone" ></form>
In the code js, the dropzone upload images only if a button with id=startUpload
is clicked.
Dropzone.autoDiscover = false;
$(function() {
var myDropzone = new Dropzone(".dropzone", {
url: "upload_hotel_image.php",
paramName: "file",
maxFilesize: 20,
maxFiles: 20,
acceptedFiles: "image/*,
autoProcessQueue: false,
});
$(document).ready(function(){
$('#startUpload').click(function(){
myDropzone.processQueue();
});
});
});
Now, how can i add more data to send with dropzone when images are uploaded, for exapmle,i need send description with every image uploaded.
//do this in your init function
this.on("sending", function(file, xhr, formData) {
$("form").find("input").each(function(){
formData.append($(this).attr("name"), $(this).val());
});

Dropzone - max files not working

I have tried setting up the limit of uploading files to only one. I have tried all the suggestions from the previous questions here but nothing worked for me. Each time I was able to upload multiple files and as many as like.
This was one of my attempts:
var token = "{{ csrf_token() }}";
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("div#dropzoneFileUpload", {
url: "/admin/upload",
params: {
_token: token
}
});
Dropzone.options.myAwesomeDropzone = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
addRemoveLinks: true,
maxFiles: 1,
init: function() {
this.on("maxfilesexceeded", function() {
if (this.files[1]!=null){
this.removeFile(this.files[0]);
}
});
},
accept: function(file, done) {
}
};
And this is how I call the scripts:
<script src="{{ asset('js/dropzone/dropzone.js') }}"></script>
<script src="{{ asset('js/image-upload.js') }}"></script>
You are splitting the dropzone configuration into two different methods. And only the first one is being used the one that contains the url option, the second, that contains the maxFiles option is ignored.
You have to either include all the configuration inside the first method that creates dropzone programmatically like this:
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("div#dropzoneFileUpload", {
url: "/admin/upload",
params: {
_token: token
},
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
addRemoveLinks: true,
maxFiles: 1,
init: function() {
this.on("maxfilesexceeded", function() {
if (this.files[1]!=null){
this.removeFile(this.files[0]);
}
});
},
accept: function(file, done) {
}
});
Or with second method that uses the dropzone autodiscover feature, if your dropzone element has the id #dropzoneFileUpload do it like this:
Dropzone.options.dropzoneFileUpload = {
url: "/admin/upload",
params: {
_token: token
},
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
addRemoveLinks: true,
maxFiles: 1,
init: function() {
this.on("maxfilesexceeded", function() {
if (this.files[1]!=null){
this.removeFile(this.files[0]);
}
});
},
accept: function(file, done) {
}
};

jQuery : Dropzone.js not working inside bootstrap modal

In my bootstrap modal I have a div panel inside of that is my form of dropzone, in every time I tried to click the modal nothing is showing. It's working properly if outside of the modal. I also try the Dropzone.autoDiscover = false; not working either.
<div class="panel-body" id="id_dropzone">
<form action="UploadImages"
class="dropzone"
id="my-awesome-dropzone" enctype="multipart/form-data">
</form>
</div>
The JS
$(document).on('click','#add_newContestant', function(e){
e.preventDefault();
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("div#id_dropzone", { url: 'UploadImages'});
});
Try this way, here i didn't use Dropzone.autoDiscover = false;
// "myAwesomeDropzone" is the camelized version of the HTML element's ID
Dropzone.options.myAwesomeDropzone = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
accept: function(file, done) {
if (file.name == "anything.jpg") {
done("false");
}
else { done(); }
}
};
and
Dropzone.options.myAwesomeDropzone = {
paramName: "file",
maxFilesize: 10,
url: 'UploadImages',
previewsContainer: "#dropzone-previews",
uploadMultiple: true,
parallelUploads: 5,
maxFiles: 20,
init: function () {
var cd;
this.on("success", function (file, response) {
$('.dz-progress').hide();
$('.dz-size').hide();
$('.dz-error-mark').hide();
console.log(response);
console.log(file);
cd = response;
});
.......
Please check this fiddle Dropzone in Modal . I think this will help you

dropzone.js ... Requeue rejected files ... trying to implement github answer into my script

When a form is submitted, if there are errors in any of the form fields (title for example) the files must be reuploaded by the user.
I am trying to implement this code into my script to fix this issue but it is not working
Dropzone.prototype.requeueFiles = function(files){
for (var i = 0, l = files.length, file; i < l; i++){
file = files[i];
file.status = Dropzone.ADDED;
file.upload.progress = 0;
file.upload.bytesSent = 0;
}
}
//...on submit
self.requeueFiles(self.files);
Note the comment:
I think you may set the status to Dropzone.QUEUED
Here is my current code:
$(document).ready(function() {
var dropzone;
Dropzone.autoDiscover = false;
dropzone = new Dropzone('#dropform', {
maxFiles: 2,
maxFilesize: 2.5,
paramName: 'photo[picture]',
headers: {
"X-CSRF-Token": $('meta[name="csrf-token"]').attr('content')
},
addRemoveLinks: true,
clickable: '.dz-default.dz-message',
previewsContainer: '.dz-default.dz-message',
thumbnailWidth: 200,
thumbnailHeight: 200,
parallelUploads: 100,
autoProcessQueue: false,
uploadMultiple: false
});
$('#item-submit').click(function(e) {
e.preventDefault();
e.stopPropagation();
if (dropzone.getQueuedFiles().length > 0) {
return dropzone.processQueue();
}
else {
return $('#dropform').submit();
}
});
dropzone.on('error', function(file, errorMessage, xhr) {
console.log('error');
$('.idea').html(errorMessage + ". Please try again. Thank you.");
});
return dropzone.on('success', function(file, responseText) {
return window.location.href = '/photos/' + responseText.id;
});
});

Dropzone.js - How to add new property to file object after uploading file

I'm using dropzone and PHP to upload and delete files. When I load my upload page I create some mockfiles with the following params: name, size, thumbnail and id. This mock is set using pre uploaded data.
So when someone click on remove file button, I call the php method that delete the image.
My problem is when the user uploads an file and tries to delete it without loading the page. When it happens, dropzone file object just can't be altered.
I'm trying:
var dropZone3 = new Dropzone("#file3",{
init: function() {
this.on('success', function (file) {
console.log(file);
file['test'] = 'test';
file.test = 'test';
console.log(file);
})
},
paramName: 'file3',
autoProcessQueue:true,
uploadMultiple: false,
parallelUploads: 1,
maxFiles: 3,
maxFilesize: 5,
addRemoveLinks: true
My problem is that the first console.log and the second one inside init on success function shows me the same file.
Anyone knows how to fix it?
Thank you in advance.
It's possible to add properties directly to file object (dropzone v4.3.0)
var dropZone = new Dropzone(document.querySelector('.js-dropzone'), {
url: '/file/upload'
});
dropZone.on('success', function (file, response) {
var res = JSON.parse(response);
if (res.result == true) {
file.test = 'test';
file.id = res.id;
}
});
Don't think you can add a property to the file object when is already uploaded, but you can add it before on the accept property:
var dropZone3 = new Dropzone("#file3", {
url: "upload.php",
init: function () {
this.on('success', function (file) {
console.log(file);
})
},
accept: function(file, done) {
file.test = "test";
return done();
},
paramName: 'file3',
autoProcessQueue: true,
uploadMultiple: false,
parallelUploads: 1,
maxFiles: 3,
maxFilesize: 5,
addRemoveLinks: true
});

Categories

Resources