base64.js 668 B

12345678910111213141516171819202122232425
  1. const fsm = wx.getFileSystemManager();
  2. function base64src(base64data, timestamp ,cb) {
  3. const FILE_BASE_NAME = 'tmp_base64src' + timestamp; //自定义文件名
  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}/${FILE_BASE_NAME}.${format}`;
  9. const buffer = wx.base64ToArrayBuffer(bodyData);
  10. fsm.writeFile({
  11. filePath,
  12. data: buffer,
  13. encoding: 'base64',
  14. success() {
  15. cb(filePath);
  16. },
  17. fail() {
  18. return (new Error('ERROR_BASE64SRC_WRITE'));
  19. },
  20. });
  21. };
  22. export { base64src };