收藏
回答

wx.uploadFile文件上传 小于2M文件上传正常,大于2M报错,需使用哪个api解决该问题?

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

  • o0o有脾气的酸奶
    o0o有脾气的酸奶
    2019-11-15

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

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

    2019-11-15
    有用
    回复 4
    • Robby
      Robby
      2019-11-15
      是前端就报错了,后台还没有接收到请求。
      2019-11-15
      回复
    • o0o有脾气的酸奶
      o0o有脾气的酸奶
      2019-11-15回复Robby
      你仔细看看提示,success回调函数你怎么理解?
      不是请求服务器返回了数据了,就进到success吗
      你在var jsonObj = JSON.parse(t_data)之前打印t_data看看
      估计没错的话,t_data应该不是json数据
      2019-11-15
      回复
    • o0o有脾气的酸奶
      o0o有脾气的酸奶
      2019-11-15回复Robby
      仔细看看,这错误提示说的是:
      在wx.uploadFile API的success回调函数里,JSON.parse里的0位置出现意外的"<"标记
      2019-11-15
      回复
    • o0o有脾气的酸奶
      o0o有脾气的酸奶
      2019-11-15回复Robby
      说明你后端报错,返回了一个错误页面的html了 给小程序这边了
      2019-11-15
      1
      回复
  • 子不语
    子不语
    2019-11-15

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

    2019-11-15
    有用
    回复 1
    • Robby
      Robby
      2019-11-15
      是前端就报错了,后台还没有接收到请求。
      2019-11-15
      回复
登录 后发表内容
问题标签