微信小程序上传图片到服务器wx.uploadFile

编程入门 行业动态 更新时间:2024-10-12 18:20:27

项目中肯定会遇到上传文件到服务器端,小程序提供了很有用的api

wxml代码:

<image  mode='widthFix' src="{{imgUrl}}"></image>
<view bindtap="getPhoto">上传图片</view>

js代码:

getPhoto(e){   //获取手机相册功能let _this = this;wx.chooseImage({      count: 1,       //上传图片数量sizeType: ['original', 'pressed'],   sourceType: ['album', 'camera'],suess(res) {console.log(res)// tempFilePath可以作为img标签的src属性显示图片const tempFilePaths = res.tempFilePaths_this.upLoad(_this, tempFilePaths);   //调用上传接口}})},upLoad(that,filePath){wx.uploadFile({url: ROOT_URL,       //换成你自己的url地址filePath: filePath[0],    //图片文件 name: 'file',header: { "Content-Type": "multipart/form-data",    //头部设置},formData: {//这里放你自己额外要带的参数},suess: function (res) {//console.log(res)var data = JSON.parse(res.data) //do something //根据你自己的项目要求,做处理this.setData({imgUrl:filePath[0]})}}) },

更多推荐

上传图片,服务器,程序,微信小,wx

本文发布于:2023-05-29 03:38:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/335890.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传图片   服务器   程序   微信小   wx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!