当前位置: 首页 > 工具软件 > tus > 使用案例 >

tus上传文件-方法

祁乐邦
2023-12-01

tus–demo1:以下为简易版tus使用方法

var fs = require('fs')
var tus = require('tus-js-client') // 引入tus插件

var path = `${__dirname}/../../README.md`
var file = fs.createReadStream(path) // 获取文件对象
var size = fs.statSync(path).size

var options = {
  endpoint: 'https://tusd.tusdemo.net/files/',//请求路径
  metadata: { // 请求约定的属性字段
    filename: 'README.md',
    filetype: 'text/plain',
  },
  uploadSize: size,
  onError (error) {
    throw error
  },
  onProgress (bytesUploaded, bytesTotal) {
    var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
    console.log(bytesUploaded, bytesTotal, `${percentage}%`)
  },
  onSuccess () {
    console.log('Upload finished:', upload.url)
  },
}

// 传入文件对象,实例化上传对象
var upload = new tus.Upload(file, options)
upload.start()
 类似资料: