base64src.js 642 B

123456789101112131415161718192021222324
  1. const fsm = wx.getFileSystemManager();
  2. const FILE_BASE_NAME = 'tmp_base64src'; //自定义文件名
  3. function base64src(base64data, cb) {
  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: 'binary',
  14. success() {
  15. cb(filePath);
  16. },
  17. fail() {
  18. return (new Error('ERROR_BASE64SRC_WRITE'));
  19. },
  20. });
  21. };
  22. export { base64src };