微信小程序上传Word文档、PDF、图片等文件

<view class="main" style="border:none">
    <view class="title">
        附件上传
    </view>
    <view class="list">
        <view class="imglist">
            <view class="imgitem">
                <image bindtap="upload_word" data-id="1" src="../../resource/images/pub.png"></image>
            </view>
        </view>
    </view>
</view>
Page((i = {
    data: {
        uploadfilelist: "",
        word_path: "",
        file_type: "",
    },

    upload_word: function() {
        var e = o.util.geturl({
            url: "后端处理接口"
        });
        var i = this;
        wx.chooseMessageFile({
            count: 1,
            type: ‘all‘,
            success (res) {
              // tempFilePath可以作为img标签的src属性显示图片
              const tempFilePaths = res.tempFiles;
              i.setData({
                word_path: tempFilePaths[0].name
              })
              wx.showToast({
                icon: "loading",
                title: "正在上传"
              });
              wx.uploadFile({
                url: e,
                filePath: tempFilePaths[0].path,
                name: "file",
                header: {
                    "Content-Type": "multipart/form-data"
                },
                formData: {
                    session_token: wx.getStorageSync("session_token"),
                    file_name: tempFilePaths[0].name
                },
                success: function(a) {
                    var e = JSON.parse(a.data);
                    if (200 == a.statusCode){
                        i.setData({
                            file_type: e.data.file_type,
                            uploadfilelist: e.data.path,
                            word_path: e.data.path
                        })
                    }  else {
                        wx.showModal({
                            title: "提示",
                            content: "上传失败",
                            showCancel: !1
                        });
                    }
                },
                fail: function(a) {
                    wx.showModal({
                        title: "提示",
                        content: "上传失败",
                        showCancel: !1
                    });
                },
                complete: function() {
                    wx.hideToast();
                }
              });
            }
        })
    },
}), i));

后端根据不同的文件后缀判断文件属于image或者video或者file,做不同的存储操作

微信小程序上传Word文档、PDF、图片等文件

上一篇:【转】android资源目录---assets与res/raw区别


下一篇:IBM Rational Appscan: Part 2 ---reference