• 微信小程序上传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,做不同的存储操作

  • 相关阅读:
    第二章—数据类型字符串str
    第二章—数据类型列表list
    第二章—编码
    第二章——进制
    ConfigParser模块
    描述符__get__,__set__,__delete__
    面向对象 ,特殊成员和魔法方法
    异常处理
    反射
    绑定方法与非绑定方法
  • 原文地址:https://www.cnblogs.com/Jessie-candy/p/13032444.html
Copyright © 2020-2023  润新知