要实现多个图片的上传,你可以对代码进行一些修改。以下是一个示例,用于支持上传多个图片:
function OutsourceFileUploadNew(init) {
init = init || {};
init.title = lang('upload attachment');
var self = new Window(init).setWidth('40%').setDestroyOnHide(true);
self.type = 'OutsourceFileUploadNew';
self.attr("id", self.type + ggNamespace.getCounter());
self.fileInput = new FileUploader({
title: lang("file name"),
name: "uploadfiles",
accept: ".jpg,.docx,.doc,.pdf",
required: true,
multiple: true // 添加multiple属性以支持多文件上传
});
self.fileInput.addChangeHandler(function() {
var files = self.fileInput.getValue();
for (var i = 0; i < files.length; i++) {
var ext = files[i].name.match(/\.([^\.]+)$/)[1];
switch (ext) {
case 'jpg':
case 'docx':
case 'doc':
case 'pdf':
break;
default:
files.splice(i, 1); // 如果有非法文件类型,则从数组中删除该文件
i--; // 调整索引以确保遍历所有文件
break;
}
}
if (files.length > 0) {
self.uploadButton.enable(); // 如果仍然有合法的文件,启用上传按钮
} else {
self.uploadButton.disable(); // 如果没有合法的文件,禁用上传按钮
new Bootbox()
.setDestroyOnHide(true)
.alert(lang('warn'), lang('Invalid attachment type! Only .jpg, .docx, .doc, .pdf formats supported.'));
}
});
self.uploadButton = new Button({
title: lang('upload'),
type: 'primary'
});
self.cancel = new Button({
title: lang('cancel'),
type: 'primary'
});
self.content = new VLayout();
self.footer = new HLayout();
self.form = new DynamicForm();
self.form.setItems({
fileInput: self.fileInput,
});
self.content.addMembers(self.form);
self.footer.fillSpaceBefore();
self.footer.setRowSpacing('5px');
self.setContent(self.content);
self.setFooter(self.footer);
// 修改上传处理程序
ggNamespace.functions.AgencyOutsideImportFileUploadWindow_UploadFileHandler = function(init) {
var self = this;
var arr = [];
if (self.form.validate()) {
var formData = new FormData($('#' + self.form.attr("id")).find('form')[0]);
formData.append("id", init.id);
var url = "../d/supplement/savePicture";
// 禁用按钮,防止重复上传
self.uploadButton.disable();
self.cancel.disable();
$.ajax({
url: url,
method: 'post',
data: formData,
enctype: 'multipart/form-data',
processData: false,
contentType: false,
dataType: 'json',
success: function(result) {
console.log('result', result);
if (result.success) {
new Bootbox()
.setWidth('15vw')
.alert(lang('alert'), lang('succeed'));
arr.push({
repaymentName: result.data.repaymentName,
repaymentPath: result.data.repaymentPath
});
// 通知事件
self.trigger('OutsourceContractImportFileUploadNew import success', arr);
self.hide();
} else {
new Bootbox()
.setWidth('15vw')
.alert(lang('alert'), result.message);
}
},
error: function(xhr, ajaxOptions, thrownError) {
ggNamespace.showError(thrownError);
}
});
}
};
// 修改上传按钮的点击处理程序
self.uploadButton.addClickHandler(function(){
self.uploadFileHandler(init);
});
self.cancel.addClickHandler(function(){
self.hide()
})
return self;
}
这样,你就可以在表单中选择多个文件进行上传。注意,修改后的代码需要与其他相关部分一起使用,以确保整体功能正常运行。
内容由零声教学AI助手提供,问题来源于学员提问