你的代码似乎是一个用于上传文件的 JavaScript 组件,但在多个地方存在语法错误和不完整的部分。以下是修复和补充后的版本:
function OutsourceFileUploadNew(init) {
init = init || {};
init.title = lang('upload attachment');
var self = new Window(init).setWidth('40%').setDestroyOnHide(true);
self.type = '0utsourceFileUploadNew';
self.attr("id", self.type + ggNamespace.getCounter());
self.fileName = new SmallTextItem({
title: lang("file name"),
name: "uploadfile"
}).setType("file")
.setAccept(".jpg,.jpeg,.png,.gif,.bmp")
.setRequired(true);
// 添加文件类型验证
self.fileName.addChangeHandler(function() {
var extMatch = self.fileName.getValue().match(/\.([^\.]+)$/);
if (extMatch) {
var ext = extMatch[1].toLowerCase();
switch(ext) {
case 'jpg':
case 'jpeg':
case 'png':
case 'gif':
case 'bmp':
break;
default:
// 提示无效附件类型
new Bootbox().alert(lang('warn'), lang('invalid file type! Only supports jpg, jpeg, png, gif, bmp'));
self.fileName.setValue('');
return; // 结束函数执行
}
}
});
self.uploadButton = new Button({ title: lang('upload'), type: 'primary' });
self.cancel = new Button({ title: lang('cancel'), type: 'default' });
self.content = new VLayout();
self.footer = new HLayout();
self.form = new DynamicForm();
// 设置表单项
self.uploadFileHandler = ggNamespace.functions.AgencyOutsideImportFileUploadWindow.UploadFileHandler;
self.form.setItems(self.fileName);
// 添加内容到布局
self.content.addMember(self.form);
// 添加按钮到底部布局
self.footer.addMember(self.uploadButton).addMember(self.cancel);
// 填充空间设置行间距
self.footer.fillSpaceBefore();
self.footer.setRowSpacing('5px');
// 设置窗口内容与脚部
self.setContent(self.content);
self.setFooter(self.footer);
// 上传按钮点击事件处理
self.uploadButton.addClickHandler(function() {
self.uploadFileHandler(init);
});
// 取消按钮点击事件处理
self.cancel.addClickHandler(function() {
self.hide();
});
return self;
}
// 文件上传处理函数实现
ggNamespace.functions.AgencyOutsideImportFileUploadWindow.UploadFileHandler = function() {
var self = this;
let arr = [];
if (self.form.validate()) {
var formData = new FormData($('#' + this.form.attr("id")).find('form')[0]);
formData.append("id", init.id);
var url="../d/exemptionReduction/saveAttachment";
// 禁用上传和取消按钮以防止重复提交
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({
fileName: result.data.fileName,
obsPath: result.data.obsPath,
tableId: result.data.tableId
});
// 触发成功事件并隐藏窗口
self.trigger('0utsourceContractImportFileUploadNew import success', arr);
self.hide();
} else {
new Bootbox().setWidth('15vw').alert(lang('alert'), result.message);
}
}
}).fail(function(xhr, ajaxOptions, thrownError) {
ggNamespace.showError(thrownError);
});
}
};
修正和补充的要点:
- 语法错误:修复了若干处语法错误,比如括号、引号配对等问题。
- 文件类型检查:添加了对文件后缀名的有效性检查,如果不符合要求,会提示用户并清空输入框。
- AJAX 请求:确保在请求成功或失败时都能给出反馈,增加了一些日志输出。
希望这个修正能够帮助你更好地理解如何实现文件上传功能。如果有其他需求或进一步的问题,请随时告诉我!
内容由零声教学AI助手提供,问题来源于学员提问