请问一下小程序中图片的上传方法?
发布于 6 年前 作者 yaoqiang 14860 次浏览 来自 问答

看了一下官方的API,

wx.chooseImage({
  success: function(res) {    var tempFilePaths = res.tempFilePaths
    wx.uploadFile({
      url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
      filePath: tempFilePaths[0],
      name: 'file',
      formData:{        'user': 'test'
      },
      success: function(res){        var data = res.data        //do something
      }
    })
  }
})

但是我的图片就是上传不到数据库中,这是我写的代码:

// pages/comment/comment.js

var app = getApp();

Page({

  /**

   * 页面的初始数据

   */

   data: {

     // openid: app.globalData.openid,

     userInfo: {},

     tempFiles: [],

     imglist:[],

     // tempFilePaths:[],

     name: ‘所在位置’,

     address:’’,

     img:"…/…/images/comment/dingwei1_03.png"

  },

  /**

   * 生命周期函数–监听页面加载

   */

  onLoad: function () {

     this.setData({

      userInfo:app.globalData.userInfo

    })

  },

  onShow:function(){

 

  },

  /**

   * 生命周期函数–监听页面初次渲染完成

   */

  onReady: function () {

 

  },

  // 点击选择图片

  chooseimage: function () {

      console.log(‘点击选择图片’);

     var _this = this;

     wx.chooseImage({

        count: 3, // 默认9  

        sizeType: [‘original’, ‘compressed’], // 可以指定是原图还是压缩图,默认二者都有  

        sourceType: [‘album’, ‘camera’], // 可以指定来源是相册还是相机,默认二者都有  

        success: function (res) {

           console.log(res);  

          var tempFilePaths = res.tempFilePaths;

            _this.setData({

              imglist:tempFilePaths

            })

 

        }

     })

  },

 

  //获取位置

  bindLocation: function () {

     var that = this;

     wx.chooseLocation({

        success: function (res) {

          console.log(res);

           that.setData({

              name: res.name,

              address: res.address,

              img: “…/…/images/comment/location.png”

           });

        }

     })

  },

//表单提交事件

  bindFormSubmit: function (e) {

      var that = this;

     if ( app.globalData.userInfo.openid ){     //如果已经登录成功

        var title = e.detail.value.title;

        var content= e.detail.value.content;

        var position= that.data.name;

        var openid=app.globalData.userInfo.openid;

         var  imglist= that.data.imglist ;

         if (title==’’){

            wx.showModal({

               title: ‘提示’,

               content: ‘标题不能为空’,

               showCancel:false

            })

         } else if (content==’’){

            wx.showModal({

               title: ‘提示’,

               content: ‘请输入你要发表的内容’,

               showCancel: false

            })

         } else {

            wx.request({

               method: “post”,

               url: ‘https://www.zsjinrong.cn/index.php/index/post/postHandle’,

               data: {

                  title: title,

                  content: content,

                  position: position,

                  openid: openid,

                  images: imglist

               },

               success: function (res) {

                  console.log(res.data.date)

                   if(imglist != ‘’){

                    //开始插入图片

                    for (var i = 0; i <imglist.length; i++) {

 

                      console.log(imglist[i]);

                      // // 上传图片

                      wx.uploadFile({

                         url: ‘https://www.zsjinrong.cn/index.php/index/post/addPostImg’,

                         filePath:imglist[i],

                         name: ‘images’,

                         header: {

                            “Content-Type”: “multipart/form-data”

                         },

                         formData:{

                          postId: res.data.date

                         },

                         success: function (res) {

                          if(i>=imglist.length){

                             that.setData({

                              imglist:[]

                             })

                            wx.showToast({

                               title: ‘图片导入成功’,

                               icon: ‘success’,

                               duration: 2000

                            })

                            //发表成功后跳到首页显示

                            wx.switchTab({

                               url: ‘…/index/index’,

                            });

                         }

                       }

                      })

                    }

                     console.log(imglist);

                  }

 

               }

            });

         }

     }else{     //还没有登录提示登录

        wx.showModal({

           title: ‘提示’,

           content: ‘您是否授权登录?’,

           confirmText: ‘授权’,

           cancelText:“拒绝”,

           success: function (res) {

              if (res.confirm) {

                 console.log(‘用户点击授权’);

 

                 app.getUserOpenId();//获取openid                

              } else if (res.cancel) {

                 console.log(‘用户点击取消’)

                 wx.showToast({

                    title: ‘取消登录’,

                    icon: ‘success’,

                    duration: 500

                 })

              }

           }

        })

     }

  

  }

})

请各位大神帮忙看看

4 回复

请问一下怎么解决的,我的上传之后状态码显示200,但是报错显示参数无效

请确认您的上传格式与服务器保持正确一致,以及是否有相关错误信息呢?

问题已解决

服务器的格式:png 、jpg、gif 格式。我上传的图片格式都一样的啊,filePath中是tempFilePaths[0]吧,formData中传的是帖子的id

回到顶部