收藏
回答

微信小程序上传文件到Django后台失败?

计划使用wx.chooseMessageFile和wx.uploadFile两个接口将文件上传到Django服务器,设置好POST url后发现一直,一直没有成功,console.log(res.data)查看发现服务器返回page not find错误,可是我浏览器get请求明明有返回,请问这到底是什么错误,要怎么解决呢?

以下是我的代码:

wx.chooseMessageFile({
      count: 1,
      type'file',
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFiles;
        var fileName = tempFilePaths[0].name//文件名称
        var filePath = tempFilePaths[0].path//文件路径
        var size = tempFilePaths[0].size / 1024 / 1024//文件大小
        wx.showModal({
          cancelColor: 'cancelColor',
          title:"确认文件",
          content:fileName,
          success (res){
            if (res.confirm){
              wx.uploadFile({
                filePath: filePath,
                name: 'file',
                url: 'https://www.xxxxxxxx/upload',//请求url
                success (res) {
                  if (res.data == "Yes"){
                    wx.showModal({
                      cancelColor: 'cancelColor',
                      showCancel:false,
                      title:"状态",
                      content:"上传成功!"
                    })
                  }
                  else {
                    wx.showModal({
                      cancelColor: 'cancelColor',
                      showCancel:false,
                      title:"状态",
                      content:"上传失败!"
                    })
                  }
                },
                fail (){
                  wx.showModal({
                    cancelColor: 'cancelColor',
                    showCancel:false,
                    title:"状态",
                    content:"上传失败!"
                  })
                }
              })
            }
          }
        })
      }
    })
回答关注问题邀请回答
收藏

1 个回答

  • (ー_ー)!!
    (ー_ー)!!
    2021-01-26

    这是后台视图代码

    # 上传文件

    def upload(req):

        if req.method == "POST":

            file = req.FILES.get('file',None)

            if not file:

                return HttpResponse("Yes")

            else:

                return HttpResponse("yes")

        else:

            return HttpResponse("No")

    2021-01-26
    有用
    回复 2
    • (ー_ー)!!
      (ー_ー)!!
      2021-01-26
      已经解决
      2021-01-26
      回复
    • 一口
      一口
      2022-02-11回复(ー_ー)!!
      你好,请问怎么解决的?
      2022-02-11
      回复
登录 后发表内容
问题标签