uploadFile

Updated at:

This topic describes how to use the uploadFile method to upload files.

Method definition

mpserverless.file.uploadFile(options: object): Promise<Result>

Request parameters

The options parameter is defined as follows:

Field

Type

Required

Description

filePath

String

Yes

The path of the local file. The software development kit (SDK) automatically generates the file name, file type, and file size based on this path.

meta

Object

No

Custom key-value pairs for the file's response header. For example, if you define "user-id": "123456", the response header for accessing the file will include x-oss-meta-user-id: 123456.

cloudPath

String

No

The path of the file in the cloud. The path must start with a forward slash (`/`), such as "/static/image/hello.jpg". If a file already exists at this path, it is automatically overwritten.

Note

The SDK version must be 3.1.5 or later.

Important
  • A single file must be smaller than 100 MB.

  • Supported file formats:

    • Images: .jpg, .jpeg, .png, .gif, .bmp, .wbmp, .svg, and .image.

    • Audio: .audio, .au, .mp2, and .mp3.

    • Video: .3gpp, .mp4, .mpeg, .flv, .avi, .wmv, .mpg, and .mpga.

    • Documents: .pdf, .xls, .xlsx, .doc, .docx, .ppt, .pptx, and .txt.

Return parameters

Field name

Type

Description

fileId

String

The file ID.

Note

The SDK version must be 3.1.3 or later.

filePath

String

The relative path of the file in the cloud.

fileUrl

String

The URL of the file after it is uploaded.

Examples

File upload from an Alipay mini program:

/* Image upload example */
my.chooseImage({
    chooseImage: 1,
    success: res => {

        const path = res.apFilePaths[0];
        const options = {
            filePath: path,
        };

        mpserverless.file.uploadFile(options)
        .then(res => { 
            console.log(res);  
        })
        .catch(err => {  
            console.log(err);
        });

    },
});

/* File upload example */
my.downloadFile({
    url: 'URL of the online file',
    success: res => {
        const path = res.apFilePath;
        const options = {
            filePath: path,
        };
        mpserverless.file.uploadFile(options)
        .then(res => { 
            console.log(res);  
        })
        .catch(err => {  
            console.log(err);
        });
    },
});

Sample output:

{
    "fileId": "5d916ee7-869f-4b90-8224-2ad15e259867",
    "fileUrl": "https://mp-…storage/2e7acad6-2212-4863-aaa7-4e89d7d8df4c.png", 
    "filePath": "cloudstorage/2e7acad6-2212-4863-aaa7-4e89d7d8df4c.png"
}

Example of uploading a file from a WeChat mini program:

/* Image upload example */
wx.chooseImage({
    count: 1,
    sizeType: [ 'original', 'compressed' ],
    sourceType: ['album', 'camera'],
    success: res => {

        const path = res.tempFilePaths[0];
        const options = {
            filePath: path,
        };

        mpserverless.file.uploadFile(options)
        .then(res => { 
            console.log(res);  
        })
        .catch(err => {  
            console.log(err);
        });

    },
});

/* File upload example */
wx.downloadFile({
    url: 'URL of the online file',
    success: res => {
        const path = res.tempFilePath;
        const options = {
            filePath: path,
        };
        mpserverless.file.uploadFile(options)
        .then(res => { 
            console.log(res);  
        })
        .catch(err => {  
            console.log(err);
        });
    },
});

Example of a file upload from a DingTalk mini program:

/* Image upload example */
dd.chooseImage({
    chooseImage: 1,
    success: res => {

        const path = res.apFilePaths[0];
        const options = {
            filePath: path,
        };

        mpserverless.file.uploadFile(options)
        .then(res => { 
            console.log(res);  
        })
        .catch(err => {  
            console.log(err);
        });

  },
});

/* File upload example */
dd.downloadFile({
    url: 'URL of the online file',
    success: res => {
        const path = res.filePath;
        const options = {
            filePath: path,
        };
        mpserverless.file.uploadFile(options)
        .then(res => { 
            console.log(res);  
        })
        .catch(err => {  
            console.log(err);
        });
    },
});