微信小程序上传文件及图片(可以预览)

最近在写小程序项目,碰到了一个需求,需要用户可以上传各种类型的文件和图片,展示在页面上,并且点击还可以进行预览,就找了找微信小程序官网,写了一个例子,分享一下

直接看代码:

wxml:

 <view>
      // 上传按钮
     <button class="upload-btn" bindtap='uploadFile'>
         <image src="../../../images/icon-upload.png"></image>上传相关资料
     </button>
      // 展示
     <view class="flex enclosure" wx:for="{{fileArray}}" wx:key="index">
         <view class="flex" bindtap="previewFile" data-path="{{item.path}}">
            // 通过filter根据不同类型展示不同图片
             <image style="width:80rpx;height:80rpx;margin-right: 18rpx;" src= 
              {{filters.picture(item.name)}}"></image>
                // 文件名
             <text style="width: 400rpx;overflow:hidden;text-overflow:ellipsis;white-                                        
              space:nowrap; ">{{item.name}}</text>
         </view>
     </view>
 </view>

wxss:

// 样式大家可以自己写
.upload-btn {
    height: 84rpx;
    color: #5f9be7;
    font-size: 28rpx;
    background-color: rgba(95, 155, 231, 0.1);
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 16rpx 0 36rpx;
}

.upload-btn image {
    width: 34rpx;
    height: 34rpx;
    margin-right: 20rpx;
}

.flex {
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #666;
}

.enclosure {
    margin-bottom: 40rpx;
}

js:

// 文件上传
        uploadFile() {
            wx.chooseMessageFile({
                count: 10,     //选择文件的数量
                type: 'all',   //选择文件的类型
                success: (res) => {
                    this.setData({
                        fileArray: this.data.fileArray.concat(res.tempFiles)
                    })
                }
            })
        },

// 预览附件
previewFile(e) {
var string = ''
string = e.currentTarget.dataset.path.substring(e.currentTarget.dataset.path.indexOf(".") + 1)
      if (string == 'png' || string == 'jpg' || string == 'gif' || string == 'jpeg') {
                // 图片预览
                var arr = []
                arr.push(e.currentTarget.dataset.path)
                wx.previewImage({
                    current: e.currentTarget.dataset.path,
                    urls: arr
                })
            } else {
                // 文件预览
                wx.openDocument({
                    fileType: string, // 文件类型
                    filePath: e.currentTarget.dataset.path, // 文件地址
                    success: function (res) {
                        console.log('成功')
                    },
                    fail: function (error) {
                        console.log("失败")
                    }
                })
            }
        },

有问题和建议欢迎大家留言

  • 6
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在微信小程序上传文件,你可以使用 `wx.chooseImage` 或 `wx.chooseFile` 方法来上传图片或其他文件。 1. `wx.chooseImage` 方法可以让用户选择图片并上传至服务器,代码示例: ```js wx.chooseImage({ count: 1, // 最多可以选择的图片张数,默认1张 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { // 返回选定照片的本地文件路径列表,tempFilePaths可作为img标签的src属性显示图片 var tempFilePaths = res.tempFilePaths; wx.uploadFile({ url: '服务器地址', // 上传接口地址 filePath: tempFilePaths[0], // 上传文件路径 name: 'file', // 上传文件名称 success: function (res) { // 上传成功后的处理逻辑 } }) } }) ``` 2. `wx.chooseFile` 方法可以让用户选择文件并上传至服务器,代码示例: ```js wx.chooseFile({ success(res) { // 选择文件后的处理逻辑 wx.uploadFile({ url: '服务器地址', // 上传接口地址 filePath: res.tempFilePaths[0], // 上传文件路径 name: 'file', // 上传文件名称 success: function (res) { // 上传成功后的处理逻辑 } }) } }) ``` 要预览上传的图片或文件,可以使用 `wx.previewImage` 方法来预览图片,代码示例: ```js wx.previewImage({ current: '当前图片的url', // 当前显示图片的链接,不填则默认为urls的第一张 urls: ['图片1的url', '图片2的url'] // 需要预览图片链接列表 }) ``` 对于其他类型的文件,可以使用 `wx.openDocument` 方法来打开预览,代码示例: ```js wx.openDocument({ filePath: '文件的本地路径', success: function (res) { // 打开文档成功的处理逻辑 } }) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值