上传文件大于2M,提示错误信息,(提示与json格式无关,是api内部限制了上传文件大小),需用哪个api能解决上传文件大小问题(上传文件格式,pdf,word,xlsx,ppt)
Unexpected token < in JSON at position 0;at api uploadFile success callback function
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at success (http://127.0.0.1:15704/appservice/pages/panel/panel.js:264:32)
附代码:
wx.chooseMessageFile({
count: 1,
//type: 'file',
success(res) {
// tempFilePath可以作为img标签的src属性显示图片
console.log("res:", res)
const tempFiles = res.tempFiles;
var fileinfo = tempFiles[0]// tempFiles[idx]
console.log("fileinfo:", fileinfo);
wx.uploadFile({
header: {
'content-type': 'multipart/form-data',
//'Content-Type': 'application/x-www-form-urlencoded'
},
// method: 'POST',
// header: { 'content-type': 'application/x-www-form-urlencoded' },
url: constant.root_upload_url,
filePath: fileinfo['path'],
name: 'file',
formData: {
'userid': userid,
'menuid': currentMenuId,
'filename': fileinfo['name'],
},
success(res) {
const t_data = res.data
var jsonObj = JSON.parse(t_data);
//刷新文件列表
if (!jsonObj['errorFlag']) {
wx.showModal({
title: '提示信息',
content: jsonObj.errorMsg,
})
} else {
//上传成功,刷新文件列表
that.reloadFolderList(currentMenuId);
}
},
error(error) {
console.log("error:", error)
wx.showModal({
title: '提示信息',
content: '抱歉,数据解析异常,请稍后重试!',
})
//do something
}
}
)
// }
以错误提示来看,不是wx.uploadFile的锅
是你后端返回的不是json数据导致JSON.parse报错
不是请求服务器返回了数据了,就进到success吗
你在var jsonObj = JSON.parse(t_data)之前打印t_data看看
估计没错的话,t_data应该不是json数据
const t_data = res.data
console.log(t_data) // 这里打印t_data看看
var jsonObj = JSON.parse(t_data)
在wx.uploadFile API的success回调函数里,JSON.parse里的0位置出现意外的"<"标记
要不你先问问你们后端(服务器那边)允许的大小?