index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. export const getImageUrl = (uri) => {
  2. return `${process.env.UNI_FILE_PREFIX}${uri}`;
  3. };
  4. export const viewFileFn = (fileUrl, fileName, fileSuffix) => { // 文件url 文件名 文件后缀
  5. uni.showLoading({
  6. title: '加载中…',
  7. mask: true,
  8. });
  9. let downloadTask = uni.downloadFile({
  10. url: fileUrl,
  11. success: function (res) {
  12. console.log('downloadFile 成功', res);
  13. var tempFilePath = res.tempFilePath; // 下载成功后的临时文件路径
  14. var newFilePath = `${wx.env.USER_DATA_PATH}/${fileName}${fileSuffix}`; // 新的文件路径,USER_DATA_PATH 是小程序的本地文件存储目录
  15. uni.saveFile({
  16. tempFilePath: tempFilePath,
  17. filePath: newFilePath,
  18. success: function (res) {
  19. console.log('saveFile 成功', res.savedFilePath);
  20. uni.openDocument({
  21. filePath: res.savedFilePath, // 打开新的文件路径
  22. showMenu: false,
  23. success: function (res) {
  24. console.log('openDocument 成功', res);
  25. },
  26. fail: function (err) {
  27. console.log('openDocument 失败', err);
  28. },
  29. });
  30. uni.hideLoading();
  31. },
  32. fail: function (err) {
  33. console.log('saveFile 失败', err);
  34. uni.hideLoading();
  35. },
  36. });
  37. },
  38. fail: function (res) {
  39. console.log('downloadFile 失败', res);
  40. uni.hideLoading();
  41. },
  42. });
  43. };