【发布时间】:2014-11-08 22:35:03
【问题描述】:
插件“blueimp/jQuery-File-Upload”让我抓狂。 在表单中,我有 2 个文件输入:一个用于图像,另一个用于文档。
<form id="file-upload">
<!--File 1 : Image -->
<input id="file-img" name="file1" type="file">
<input id="txt-file-img" type="text" class="form-control" style="background:white;" readonly>
<!--File 2 : Document -->
<input id="file-doc" name="file2" type="file">
<input id="txt-file-doc" type="text" class="form-control" style="background:white;" readonly>
</form>
我想根据用于选择文件的输入应用不同的测试(大小和类型)。 我在文档中找不到如何管理这种简单的情况。
$('#file-upload').fileupload({
dataType: 'json',
autoUpload: false,
add: function (e, data) {
var uploadErrors = [];
var acceptImgTypes = /^image\/(gif|jpe?g|png)$/i;
var acceptDocTypes = /^application\/(pdf|msword)$|^text\/plain$/i;
/**TEST HERE**/
/** How to make the distinction between data coming from file-img and data coming from file-doc ? ***/
if(data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) {
uploadErrors.push('Not an accepted file type');
}
if(data.originalFiles[0]['size'].length && data.originalFiles[0]['size'] > 4000000) {
uploadErrors.push('File size is too big');
}
if(uploadErrors.length > 0) {
alert(uploadErrors.join("\n"));
} else {
/** Same problem here I need to make the distinction in order to update the right input[type=text]***/
$.each(data.files, function (index, file) {
$("#txt-file-img").val(file.name);
$("#txt-file-doc").val(file.name);
});
$("#btn_add_valid").on('click',function () {
$("#loading_modal").modal("show");
data.submit();
});
}
},
done: function (e, data) {
$("#loading_modal").modal("hide");
$("#output").html('<p class="sucess">OK!</p>');
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#upload-progress .bar').css(
'width',
progress + '%'
).text(
progress + '%'
);
},
fail: function (e, data) {
$("#output").html('<p class="error">FAIL!</p>');
}
});
你知道如何使用这个插件管理多个输入[type=file]?
非常感谢您的帮助
【问题讨论】:
-
blueimp/jQuery-File-Upload Wiki 现在有一个条目:github.com/blueimp/jQuery-File-Upload/wiki/…
标签: javascript jquery jquery-plugins jquery-file-upload blueimp