我在我的Code Igniter项目中使用了dropzone。
随着文件的每次拖动,dropzone都会创建一个ajax请求,我的文件也会存储在服务器上。但现在,我的要求是我想在文件旁边发送附加数据(DYNAMIC)。使用params,只能发送静态数据,但我想发送的数据每次都会发生变化。
这是我的代码的样子:
<script>
Dropzone.autoDiscover = false;
Dropzone.options.attachment = {
init: function(){
this.on('removedfile',function(file){
// console.log('akjsdhaksj');
var fileName = file.name;
$.ajax({
type: 'POST',
url: "<?php echo BASE_URL.'index.php/admin/mail_actions/deleteFile' ?>",
data: "id="+fileName,
dataType: 'html'
});
});
},
// params: {
// customerFolder: $('#toValue').substr(0, toValue.indexOf('@')),
// },
dictDefaultMessage:"Click / Drop here to upload files",
addRemoveLinks: true,
dictRemoveFile:"Remove",
maxFiles:3,
maxFilesize:8,
}
$(function(){
var uploadFilePath = "<?php echo BASE_URL.'index.php/admin/mail_actions/uploadFile' ?>";
var myDropzone = new Dropzone("div#attachment", { url: uploadFilePath});
});
</script>
无论如何我能实现吗?
我知道了。这是我必须使用的
myDropzone.on('sending', function(file, xhr, formData){
formData.append('userName', 'bob');
});
Abhinav有正确和有效的答案我只想给出在选项对象中使用它的第二个选项(例如,如果你在一个页面上有多个Dropzone部分。)
myDropzone.options.dropzoneDivID = {
sending: function(file, xhr, formData){
formData.append('userName', 'Bob');
}
};
如果你有一个嵌套的有效载荷对象 - 例如要为您的文件添加名称,您的api只接受这样的内容
{
someParameter: {
image: <my-upload-file>,
name: 'Bob'
}
}
你的dropzone设置看起来像这样
var myDropzone = new Dropzone("div#attachment", {
url: uploadFilePath,
paramName: 'someParameter[image]'
});
myDropzone.on('sending', function(file, xhr, formData){
formData.append('someParameter[image]', file);
formData.append('someParameter[userName]', 'bob');
});
我只添加了这个,因为现在没有记录嵌套参数的例子。