1 回答
TA贡献1725条经验 获得超7个赞
干得好。我已经修改了您的代码,因此它包含 ID 为“过期”的输入值。您必须修改 HTML 代码,并且您的过期输入没有附加 ID。
<input type="file" name="file[]" id="file" required multiple accept="application/pdf" />
<input type="date" name="expire" id="expire" class="form-control" min="<?php echo date('Y-m-d') ?>" />
<button type="submit" id="submit" name="submit" class="btn btn-success" style="width:100%">Commencer <i class="fa fa-upload"></i>
上传.js
var app = app || {};
(function(o) {
"use strict";
// private methods
var ajax, getFormData, setProgress;
ajax = function(data) {
var xmlhttp = new XMLHttpRequest(), uploaded;
xmlhttp.addEventListener('readystatechange', function(){
if(this.readyState === 4){
if(this.status === 200){
uploaded = JSON.parse(this.response);
if(typeof o.options.finished === 'function') {
o.options.finished(uploaded);
}
} else {
if(typeof o.options.error === 'function') {
o.options.error();
}
}
}
});
xmlhttp.addEventListener('progress', function(event) {
var percent;
if(event.lengthComputable === true){
percent = Math.round((event.loaded / event.total) * 100);
setProgress(percent);
}
});
xmlhttp.open('post', o.options.processor);
xmlhttp.send(data);
}
getFormData = function(source, expire) {
var data = new FormData(), i;
for (i = 0; i < source.length; i++) {
data.append('file[]', source[i]);
}
data.append('ajax', true);
data.append('expire', expire);
return data;
};
setProgress = function(value){
if(o.options.progressBar !== undefined){
o.options.progressBar.style.width = value ? value + '%' : 0;
}
if(o.options.progressText !== undefined){
o.options.progressText.innerText = value ? value + '%' : '';
}
};
o.uploader = function(options) {
o.options = options;
if(o.options.files !== undefined){
ajax(getFormData(o.options.files.files, o.options.expire.value));
}
};
}(app));
阿贾克斯代码:
document.getElementById('submit').addEventListener('click', function(e){
e.preventDefault();
var f = document.getElementById('file'),
pb = document.getElementById('progressbar'),
pt = document.getElementById('progressbar-text'),
btn = document.getElementById('submit'),
expire = document.getElementById('expire');
btn.innerHTML = "Chargement en cours …";
btn.disabled = true;
app.uploader({
files: f,
progressBar: pb,
progressText: pt,
processor: 'upload.php',
expire: expire,
finished: function(data){
//
},
error: function() {
alert('Erreur ! veuillez réessayer SVP');
}
});
});
- 1 回答
- 0 关注
- 170 浏览
添加回答
举报