wx.uploadFile文件上传 小于2M文件上传正常,大于2M报错,需使用哪个api解决该问题?
发布于 6 年前 作者 ntao 1696 次浏览 来自 官方Issues

上传文件大于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

}

}

)

// }

2 回复

以错误提示来看,不是wx.uploadFile的锅

是你后端返回的不是json数据导致JSON.parse报错

要不你先问问你们后端(服务器那边)允许的大小?

回到顶部