Skip to content

weixin-ts / cdn/src / uploadMedia

Function: uploadMedia()

uploadMedia(options): Promise<UploadedMedia>

Defined in: cdn/src/upload.ts:81

Upload a media file to the WeChat CDN.

This handles the full upload pipeline:

  1. Compute file hash (MD5)
  2. Generate AES-128 key and file key
  3. Request pre-signed upload URL from the API
  4. Encrypt file with AES-128-ECB
  5. Upload encrypted file to CDN
  6. Return upload info for message construction

Parameters

options

UploadMediaOptions

Upload configuration

Returns

Promise<UploadedMedia>

Upload result with CDN parameters for message construction

Example

ts
import { uploadMedia } from '@weixin-ts/cdn'

const result = await uploadMedia({
  file: imageBuffer,
  toUserId: 'recipient_id',
  mediaType: 'image',
  apiOptions: { baseUrl: 'https://ilinkai.weixin.qq.com', token: '...' },
  cdnBaseUrl: 'https://novac2c.cdn.weixin.qq.com/c2c',
})
// Use result.downloadParam in message construction

Released under the MIT License.