base64src.js 739 B

1234567891011121314151617181920212223242526
  1. const fsm = wx.getFileSystemManager();
  2. function base64src(base64data, fileName) {
  3. fileName = fileName || 'file_base64src'; //自定义文件名
  4. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
  5. if (!format) {
  6. return (new Error('ERROR_BASE64SRC_PARSE'));
  7. }
  8. const filePath = `${wx.env.USER_DATA_PATH}/${fileName}.${format}`;
  9. const buffer = wx.base64ToArrayBuffer(bodyData);
  10. return new Promise((resolve, reject) =>{
  11. fsm.writeFile({
  12. filePath,
  13. data: buffer,
  14. encoding: 'binary',
  15. success() {
  16. resolve(filePath);
  17. },
  18. fail() {
  19. reject(new Error('ERROR_BASE64SRC_WRITE'));
  20. },
  21. });
  22. })
  23. };
  24. export { base64src };