C#后端接口:
public string FileUploads()
{string parameters = "";try{string path = "/tmp/";HttpPostedFile file = System.Web.HttpContext.Current.Request.Files["content"];parameters = string.Format("postData:{0}", file.ToString());if (file != null){Stream sr = file.InputStream;Bitmap bitmap = (Bitmap)Bitmap.FromStream(sr);path += file.FileName;string currentpath = System.Web.HttpContext.Current.Server.MapPath("~");bitmap.Save(currentpath + path);}return "{\"code\": 1, \"msg\": \"上传成功!\", data: \"" + strPath + "\"}";}catch (Exception ex){return "{\"code\": -1, \"msg\": \"上传图片异常:" + ex.Message + "\"}";}
}
微信小程序端:
wxml:
<view class="imgBox"><block wx:for="{{img_arr}}" wx:key="index"><view class='logoinfo'><image class='logoinfo' mode="aspectFill" src='{{item}}' data-index="{{index}}" bindtap="previewImg" bindlongpress="deleteImg" name="headimage" >image>view>block><image bindtap='getLocalityImg' class='logoinfo' src="../../images/icon/xj.png">image>
view>
<button bindtap="uploadFiles" style="margin-top:50rpx;">提交button>
wxss:
.imgBox {font-size: 28rpx;margin: 3%;background: white;padding: 30rpx;border-radius: 30rpx;display: flex;align-items: center;
}
.logoinfo {height: 180rpx;width: 180rpx;margin: 10rpx ;display: inline-block;
}
js:
Page({data: {img_arr: [], },uploadFiles(){var that = thisvar adds = that.data.img_arr;for (let i = 0; i < this.data.img_arr.length; i += 1) {wx.uploadFile({url: Url,filePath: that.data.img_arr[i],name: 'content',formData: {'user': adds},success: function (res) {let data = JSON.parse(res);if (data.code == 1) {console.log(data.data);wx.showToast({title: data.msg,duration: 3000});}else{wx.showToast({title: data.msg,duration: 3000});}}})}},getLocalityImg() {let that = this;if (this.data.img_arr.length < 5) {wx.chooseImage({count: 5 - that.data.img_arr.length, sizeType: ['original', 'compressed'],sourceType: ['album', 'camera'],success(res) {console.log(res, "上传图片")const tempFiles = res.tempFiles let answer = tempFiles.every(item => {return item.size <= 2000000})if (answer) {that.setData({img_arr: that.data.img_arr.concat(res.tempFilePaths),})}else{wx.showToast({title:'上传图片不能大于2M!',icon:'none' })}}})} else {wx.showToast({ title: '最多上传五张图片',icon: 'none',duration: 2000})}},deleteImg(e) {let that = this;let img_arr = that.data.img_arr;let index = e.currentTarget.dataset.index; wx.showModal({title: '提示',content: '确定要删除此图片吗?',success(res) {if (res.confirm) {img_arr.splice(index, 1);} else if (res.cancel) {return false;}that.setData({img_arr: img_arr});}})},previewImg(e) {let index = e.currentTarget.dataset.index;let img_arr = this.data.img_arr;wx.previewImage({current: img_arr[index],urls: img_arr})},onLoad(options) {},onReady() {},onShow() {},onHide() {},onUnload() {},onPullDownRefresh() {},onReachBottom() {},onShareAppMessage() {}
})
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!