Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@uni/file

raxjs2.7kBSD-3-Clause1.1.1TypeScript support: included

npm

readme

file

npm

File operations.

Support

browser miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram

Install

$ npm install @uni/file --save

Methods

getInfo(options)

Get file information.

Support

miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram

Parameters

Property Type Default Required Description Support
filePath String | √ The file path miniApp wechatMiniprogram bytedanceMicroApp
digestAlgorithm String md5 x The algorithm to calculate the file summary can be md5, sha1 miniApp wechatMiniprogram bytedanceMicroApp

getSavedInfo(options)

Gets saved file information.

Support

miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram

Parameters

Property Type Default Required Description Support
filePath String | √ The file path miniApp wechatMiniprogram bytedanceMicroApp

getSavedList(options)

Gets all saved file information.

Support

miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram

openDocument(options)

Open a file preview on the new page.

Support

miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram

Parameters

Property Type Default Required Description Support
filePath String | √ The file path miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram
fileType String | x The file Type miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram

removeSaved(options)

Delete a saved file.

Support

miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram

Parameters

Property Type Default Required Description Support
filePath String | √ The file path miniApp wechatMiniprogram bytedanceMicroApp

save(options)

Save the file locally.

Support

miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram

Parameters

Property Type Default Required Description Support
filePath String | √ The file path miniApp wechatMiniprogram bytedanceMicroApp

upload(options)

Upload local resources to the developer server.

Support

browser miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram

Parameters

Property Type Default Required Description Support
url String | √ Developer server address browser miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram
filePath `String File Object` | √ The path to upload the file resource. In web browser, it can be either base64 string or a File object. browser miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram
fileName String | √ File name, that is, the corresponding key, the developer in the server side through this key can get the binary content of the file. browser miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram
fileType String | √ File type supports image, video, audio (image/video/audio). Not required in browser. miniApp wechatMiniprogram bytedanceMicroApp
header Object | x The HTTP request Header browser miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram
formData Object | x Additional form data in the HTTP request browser miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram
withCredentials Boolean | x Custom withCredentials option browser
timeout Number | x Upload timeout browser wechatMiniprogram kuaiShouMiniProgram

Return

Notice: Alipay miniapp only supports client 10.1.35 and above. Please use it with caution because it will destroy one code with multiple platforms. When using it, please make sure that it is not empty, and the upload will return empty under containers that do not support UploadTask.

UploadTask

An object that can monitor upload progress change events and cancel upload tasks. Specific documents can be viewed:

  • WeChat: Link
  • ByteDance: Link
  • BaiDu: Link
  • KuaiShou: Link
  • Alipay: Same as WeChat, but missing onHeadersReceived and offHeadersReceived.
  • Web: Same as WeChat

download(options)

Download file resources locally.

Support

miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram

Parameters

Property Type Default Required Description Support
url String | √ Download file address miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram
header Object | x The HTTP request Header miniApp wechatMiniprogram bytedanceMicroApp baiduSmartProgram kuaiShouMiniProgram

Return

注意:只有微信小程序和字节跳动小程序、百度小程序、快手小程序支持,由于破坏了一码多端请谨慎使用

DownloadTask 一个可以监听下载进度变化事件和取消下载的对象 具体文档可以查看: 微信:https://developers.weixin.qq.com/miniprogram/dev/api/network/download/DownloadTask.html 字节跳动:https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/api/network/http/download-task/ 百度:链接 快手:链接

Example

import { download, getInfo, getSavedInfo, getSavedList, openDocument, removeSaved, save, upload } from '@uni/file';

// Get file information.
getInfo({
  filePath: 'https://resource/apml953bb093ebd2834530196f50a4413a87.video',
  digestAlgorithm: 'sha1',
  success: (res)=>{
    console.log(JSON.stringify(res))
  }
});

// You need to save the address to be able to use File.getsavedinfo
getSavedInfo({
  filePath: '**filePath**',
  success: (res) => {
    console.log(res.size);
    console.log(res.createTime);
  }
});

getSavedList({
  success:(res) => {
    console.log(JSON.stringfy(res));
  }
});

openDocument({
  filePath: '**filePath**',
  fileType: 'pdf',
  success: (res) => {
    console.log('open document success');
  };
});

removeSaved({
  filePath: '**filePath**',
  success:(res) => {
    console.log('remove success');
  }
});

save({
  filePath: '**filePath**',
  success:(res) => {
    console.log('save success');
  }
});

upload({
  url: 'http://httpbin.org/post',
  fileType: 'image',
  fileName: 'file',
  filePath: '**filePath**',
  success: res => {
    console.log('upload success');
  },
  fail: res => {
    console.log('upload succefailss');
  },
});

download({
  url: 'http://img.alicdn.com/tfs/TB1x669SXXXXXbdaFXXXXXXXXXX-520-280.jpg',
  success: res => {
    console.log(res.filePath);
  },
  fail: res => {
    console.log(res);
  },
});