我正在使用 ext 4.0.7。我正在尝试上传文本文件并进行处理。这是我的js代码
}, {
xtype: 'form',
id: 'uploadAccountForm',
name: 'uploadAccountForm',
border: false,
padding: '5px, 45px',
// standardSubmit: true,
renderTo: Ext.getBody(),
enctype: 'multipart/form-data',
fileUpload: true,
items: [{
xtype: 'fileuploadfield',
minWidth: 96,
name: 'file',
id: 'multipacAccountList',
fieldLabel: bundle.getMsg('label.modal.additional_tools.multipacadmin.label.accountlist'),
buttonText: bundle.getMsg('label.modal.additional_tools.multipacadmin.button.upload'),
buttonOnly: false,
vtype:'fileUpload'
}]
},{
xtype: 'button',
text: bundle.getMsg('label.modal.additional_tools.multipacadmin.button.getresults'),
minWidth: 96,
margin: 10,
name: 'multipacGet',
id: 'multipacGet',
handler: function() {
var account = Ext.getCmp('multipacAccount').getValue();
var company = Ext.getCmp('multipacCompany').getValue();
var accountList = Ext.getCmp('multipacAccountList').getValue();
var companyList = Ext.getCmp('multipacCompanyList').getValue();
var dlg = this.up('multiPacAdminDlg');
if (account !== "" && company !== "") {
Ext.MessageBox.alert('iCRM', Ext.String.format(bundle.getMsg('label.modal.additional_tools.multipacadmin.button.getresults.both')));
} else if (account !== "") {
iCRM.app.fireEvent("showMultiPacDefModal");
// getResults("account", account, dlg.down('#grid'));
} else if (company !== "") {
iCRM.app.fireEvent("showMultiPacDefModal");
// getResults("company", company, dlg.down('#grid'));
} else if (accountList !== "" && companyList !== "") {
Ext.MessageBox.alert('iCRM', Ext.String.format(bundle.getMsg('label.modal.additional_tools.multipacadmin.button.getresults.both')));
var accountList = Ext.getCmp('multipacAccountList');
accountList.superclass.setValue.call(accountList, "");
var companyList = Ext.getCmp('multipacCompanyList');
companyList.superclass.setValue.call(companyList, "");
} else if (accountList !== "") {
this.fileUpload("account");
} else if (companyList !== "") {
this.fileUpload("company");
} else {
Ext.MessageBox.alert('iCRM', Ext.String.format(bundle.getMsg('error.search.regular.missing_criteria')));
}
},
fileUpload: function(category) {
var form = "";
var url = "";
if (category === "company") {
url = "../search/uploadCompanies";
form = Ext.getCmp('uploadCompanyForm').getForm();
} else if (category === "account") {
url = "../search/uploadAccounts";
form = Ext.getCmp("uploadAccountForm").getForm();
}
debugger;
if (form.isValid()) {
form.submit({
url: url,
method:'POST',
waitMsg: 'Uploading your file...',
success:function (form, action) {
Ext.Msg.alert('Success', 'Your file has been uploaded.');
},
failure: function (form, action) {
if (action.result.code==='Invalid Record') {
Ext.Msg.alert("Error", Ext.String.format(bundle.getMsg('label.modal.additional_tools.multipacadmin.upload.records.message')));
} else {
Ext.Msg.alert("Error", "Your file has NOT been uploaded. " + action.result.code);
}
},
callback:function (form, action) {
debugger;
Ext.Msg.alert('Success', 'Your file has been uploaded. Callback');
}
});
}
}
}, {
这是我的java代码
@RequestMapping(value = "/uploadAccounts", method = {RequestMethod.POST})
public
@ResponseBody
Map<String, ? extends Object> uploadAccounts(FileUploadBean uploadItem, BindingResult result) throws IOException {
CommonsMultipartFile accountFile = uploadItem.getFile();
String[] records = processFile(accountFile);
Map<String, Object> jsonMap = new HashMap<String, Object>();
if (records != null) {
if (records.length>100){
jsonMap.put("success", Boolean.TRUE);
jsonMap.put("code", "Too many Record");
jsonMap.put("data", records);
} else {
jsonMap.put("success", Boolean.TRUE);
jsonMap.put("data", records);
}
} else {
jsonMap.put("success", Boolean.FALSE);
jsonMap.put("code", "Invalid Record");
}
return jsonMap;
}
文件已上传,我可以毫无问题地处理它。但是,当我将响应发送到我的 js 时,我得到了这个:
Ext.Error:您正在尝试解码无效的 JSON 字符串:< pre >{"data":["876567","676567","678988","654345","654321","665654","676556 ","678780","909877","454343","434343"],"成功":true}
我用萤火虫分析了我的反应,我可以看到反应只是:{“数据”:[“876567”,“676567”,“678988”,“654345”,“654321”,“665654”,“676556” ,"678780","909877","454343","434343"],"成功":true}
所以,不知何故,我添加了 < pre > 标签。我发现的唯一解决方案是修改 ext 核心,将这一行添加到 decode 函数中,以删除 < pre > 或 < embed > 标签。
Ext.each([/<\/?pre[^>]*>/g, /<\/?embed[^>]*>/g], function (s) {
json = json.replace(s, "");
});
但这不是我们正在寻找的解决方案。
我尝试了一个标准提交,但那是重定向我一个新页面、标签或 iframe,并且我没有从我的提交中达到成功/失败功能我还尝试将响应设置为 application/json contentType 或 html/ txt ......但我得到了同样的回应。
而且,据我所知,我无法执行 ajax 请求,因为我正在上传文件。那么,有什么想法或建议吗?提前致谢