WeChat applet to upload audio files to OSS cloud service

Scenario requirements

The audio file needs to be uploaded in the WeChat applet, and the audio file needs to be directly uploaded to the Alibaba Cloud OSS storage bucket

WeChat applet selection method

The ordinary wx.chooseImage api for uploading pictures cannot meet the needs of uploading audio and other files. We can use the wx.chooseMessageFile method. Select the file in the WeChat chat and upload it, so that the file format is not limited.

The following is my upload selection process for an MP3 file.

After confirming, print out the data returned by the upload successfully

We need to use the path temporary path to upload the path path to the interface to complete the file upload

WeChat applet uploads files to Alibaba Cloud OSS

Reference the encapsulated upload file uploadAliyun.js

const uploadImage = require('../../utils/uploadToAliOss/uploadAliyun.js');

upfile(){
            var _this=this
            wx.chooseMessageFile({
                count: 1,
                type: 'file',
                success(rest) {
                    console.log(rest.tempFiles[0]);
                    var filePath=rest.tempFiles[0]
                    uploadImage({
                        filePath: rest.tempFiles[0],
                        dir: '', //上传的目录
                        success: function(res) { 
                            _this.video=res
                        },
                        fail: function(res) {
                            console.log('上传失败---', res);
                        }
                    });
                }
            });
        },

File download address: https://download.csdn.net/download/qq_35946021/87578815

Notice:

By default, the audio file is uploaded. If it is a picture file or other file, the filePath parameter of the uploadAliyun.js file is a temporary path. The global search for filePath: params.filePath.path may have different parameters for you

config.js content replaced with its own OSS configuration information

var fileHost = "https://xxxx.oss-cn-shanghai.aliyuncs.com"

OSSAccessKeyId: 'xxxx',

AccessKeySecret: 'xxxx',

Guess you like

Origin blog.csdn.net/qq_35946021/article/details/129547041