Gae*_*ala 13 javascript jquery dropzone.js dropzone
我想在我的dropzone文件上传器上添加一个按钮上传.目前,在选择或拖动文件到dropzone区域后,它会直接上传文件.我想要做的是:1.选择或暂停要上传的文件.2.验证3.点击或按下上传按钮上传文件.
注意:只有在按下按钮上传后才会上传文件.
这是我的表格
<form id='frmTarget' name='dropzone' action='upload_files.php' class='dropzone'>
<div class='fallback'>
<input name='file' type='file' multiple />
</div>
<input id='refCampaignID' name='refCampaignID' type='hidden' value=\ "$rowCampaign->CampaignID\" />
</form>
Run Code Online (Sandbox Code Playgroud)
这是我的JS
Dropzone.options.frmTarget =
{
url: 'upload_files.php',
paramName: 'file',
clickable: true,
maxFilesize: 5,
uploadMultiple: true,
maxFiles: 2,
addRemoveLinks: true,
acceptedFiles: '.png,.jpg,.pdf',
dictDefaultMessage: 'Upload your files here',
success: function(file, response)
{
setTimeout(function() {
$('#insert_pic_div').hide();
$('#startEditingDiv').show();
}, 2000);
}
};
Run Code Online (Sandbox Code Playgroud)
这是我的php帖子请求
foreach ($_FILES["file"] as $key => $arrDetail)
{
foreach ($arrDetail as $index => $detail) {
//print_rr($_FILES["file"][$key][$index]);
$targetDir = "project_images/";
$fileName = $_FILES["file"]['name'][$index];
$targetFile = $targetDir.$fileName;
if(move_uploaded_file($_FILES["file"]['tmp_name'][$index],$targetFile))
{
$db = new ZoriDatabase("tblTarget", $_REQUEST["TargetID"], null, 0);
$db->Fields["refCampaignID"] = $_REQUEST["refCampaignID"];
$db->Fields["strPicture"] = $fileName;
$db->Fields["blnActive"] = 1;
$db->Fields["strLastUser"] = $_SESSION[USER]->USERNAME;
$result = $db->Save();
if($result->Error == 1){
return "Details not saved.";
}else{
return "Details saved.";
}
}else{
return "File not uploaded.";
}
}
}
Run Code Online (Sandbox Code Playgroud)
Don*_*nic 36
你需要:
添加一个按钮:
<button type="submit" id="button" class="btn btn-primary">Submit</button>
Run Code Online (Sandbox Code Playgroud)删除时,告诉Dropzone 不要自动上传文件,因为默认情况下会这样.这是使用config选项完成的autoProcessQueue
:
autoProcessQueue: false
Run Code Online (Sandbox Code Playgroud)由于Dropzone现在不会自动上传文件,因此您需要在单击按钮时手动指示它执行此操作.所以你需要一个事件处理程序来点击该按钮,告诉Dropzone进行上传:
$("#button").click(function (e) {
e.preventDefault();
myDropzone.processQueue();
});
Run Code Online (Sandbox Code Playgroud)这只是POST上传的文件,没有任何其他输入字段.您可能希望发布所有字段,例如您refCampaignID
的CSRF令牌,如果您有,等等.为此,您需要在发送之前将它们复制到POST中.Dropzone有一个sending
事件,在每个文件发送之前调用,您可以在其中添加回调:
this.on('sending', function(file, xhr, formData) {
// Append all form inputs to the formData Dropzone will POST
var data = $('form').serializeArray();
$.each(data, function(key, el) {
formData.append(el.name, el.value);
});
});
Run Code Online (Sandbox Code Playgroud)把它们放在一起:
Dropzone.options.frmTarget = {
autoProcessQueue: false,
url: 'upload_files.php',
init: function () {
var myDropzone = this;
// Update selector to match your button
$("#button").click(function (e) {
e.preventDefault();
myDropzone.processQueue();
});
this.on('sending', function(file, xhr, formData) {
// Append all form inputs to the formData Dropzone will POST
var data = $('#frmTarget').serializeArray();
$.each(data, function(key, el) {
formData.append(el.name, el.value);
});
});
}
}
Run Code Online (Sandbox Code Playgroud)
以为我也会添加一个纯香草 JS 解决方案,没有 jQuery。
/* 'dropform' is a camelized version of your dropzone form's ID */
Dropzone.options.dropform = {
/* Add all your configuration here */
autoProcessQueue: false,
init: function()
{
let myDropzone = this;
/* 'submit-dropzone-btn' is the ID of the form submit button */
document.getElementById('submit-dropzone-btn').addEventListener("click", function (e) {
e.preventDefault();
myDropzone.processQueue();
});
this.on('sending', function(file, xhr, formData)
{
/* OPTION 1 (not recommended): Construct key/value pairs from inputs in the form to be sent off via new FormData
'dropform' is the ID of your dropzone form
This method still works, but it's submitting a new form instance. */
formData = new FormData(document.getElementById('dropform'));
/* OPTION 2: Append inputs to FormData */
formData.append("input-name", document.getElementById('input-id').value);
});
}
};
Run Code Online (Sandbox Code Playgroud)
注意:设置事件侦听器(例如sending
我们在这里所做的)应该放在init
函数中。如果您要将它们放在其他地方,例如:
init: function()
{
//...
},
sending: function(file, xhr, formData)
{
//... logic before each file send
}
Run Code Online (Sandbox Code Playgroud)
这将覆盖为sending
事件侦听器提供的默认逻辑 dropzone ,并可能导致意外的副作用。只有当您知道自己在做什么时,才应该这样做。
归档时间: |
|
查看次数: |
23224 次 |
最近记录: |