ssDownload.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view></view>
  3. </template>
  4. <script>
  5. export default {
  6. name: 'ss-download',
  7. props: {
  8. fileUrl: {
  9. type: String,
  10. default: ''
  11. },
  12. fileType: {
  13. type: String,
  14. default: '', //1.预览图片,2.预览文件,3.预览视频
  15. },
  16. },
  17. data() {
  18. return {};
  19. },
  20. methods: {
  21. toDownload(item) {
  22. // #ifdef H5
  23. this.downloadH5(item.src)
  24. // #endif
  25. // #ifdef MP-WEIXIN
  26. if(item.type == 1){
  27. this.saveToPhotosAlbum(item.src)
  28. } else if (item.type == 2) {
  29. this.saveFile(item.src)
  30. }else{
  31. this.getDownVideo(item.src)
  32. }
  33. // #endif
  34. // #ifdef APP-PLUS
  35. this.saveFileToApp(item.src)
  36. // #endif
  37. },
  38. //H5下载图片到本地
  39. downloadH5(url) {
  40. uni.downloadFile({
  41. url: url, //仅为示例,并非真实的资源
  42. success: (res) => {
  43. console.log(res)
  44. if (res.statusCode === 200) {
  45. console.log('下载成功');
  46. var oA = document.createElement("a");
  47. oA.download = ''; // 设置下载的文件名,默认是'下载'
  48. oA.href = res.tempFilePath; //临时路径再保存到本地
  49. document.body.appendChild(oA);
  50. oA.click();
  51. oA.remove(); // 下载之后把创建的元素删除
  52. }
  53. }
  54. });
  55. },
  56. //微信小程序保存文档,不支持h5
  57. saveFile(url) {
  58. uni.showLoading()
  59. uni.downloadFile({ //下载文件资源到本地,返回文件的本地临时路径
  60. url: url, //网络图片路径
  61. success: (res) => {
  62. var filePath = res.tempFilePath;
  63. //保存到本地
  64. uni.saveFile({
  65. tempFilePath: filePath,
  66. success: function(res) {
  67. //res.savedFilePath文件的保存路径
  68. uni.openDocument({
  69. filePath: res.savedFilePath,
  70. fileType: 'pdf',
  71. showMenu: true,
  72. success: function(res) {
  73. uni.hideLoading()
  74. },
  75. fail: function(err) {
  76. uni.hideLoading()
  77. }
  78. });
  79. },
  80. fail(err) {
  81. console.log(err)
  82. uni.hideLoading()
  83. console.log('保存失败')
  84. }
  85. });
  86. }
  87. })
  88. },
  89. //微信小程序保存视频
  90. getDownVideo(url) {
  91. // 自定义 文件名称
  92. uni.showLoading({
  93. mask: true,
  94. title: '下载中...'
  95. })
  96. let fileName = new Date().valueOf();
  97. const task = uni.downloadFile({
  98. url: url,
  99. filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.mp4', // 拼接本地文件路径
  100. success: (res) => {
  101. let filePath = res.filePath
  102. uni.saveVideoToPhotosAlbum({
  103. filePath,
  104. success: (res) => {
  105. uni.showToast({
  106. title: '下载成功',
  107. icon: 'success',
  108. })
  109. let fileMgr = wx.getFileSystemManager();
  110. // 删除本地文件
  111. fileMgr.unlink({
  112. filePath: wx.env.USER_DATA_PATH + '/' + fileName +
  113. '.mp4',
  114. success: function(r) {
  115. console.log('unlink-getFileSystemManager')
  116. console.log(r)
  117. },
  118. })
  119. },
  120. fail(err) {
  121. uni.showToast({
  122. title: '保存失败',
  123. icon: 'none',
  124. })
  125. },
  126. complete(res) {
  127. console.log('saveVideoToPhotosAlbum-complete')
  128. console.log(res)
  129. uni.hideLoading()
  130. }
  131. })
  132. },
  133. fail(err) {
  134. uni.showToast({
  135. title: '下载失败,请稍后再试',
  136. icon: 'none',
  137. })
  138. },
  139. complete(res) {
  140. console.log('downloadFiledownloadFiledownloadFiledownloadFiledownloadFile')
  141. console.log(res)
  142. }
  143. })
  144. task.onProgressUpdate(this.onProgress)
  145. },
  146. // 提示下载进度
  147. onProgress(res) {
  148. uni.showLoading({
  149. mask: true,
  150. title: res.progress ? '下载中' + res.progress + "%" : '下载中...'
  151. })
  152. },
  153. //微信小程序保存图片(此处也可用于app保存图片或视频)
  154. saveToPhotosAlbum(url) {
  155. // #ifdef APP-PLUS
  156. var showLoading = plus.nativeUI.showWaiting("正在下载"); //创建一个showWaiting对象
  157. // #endif
  158. let task = uni.downloadFile({
  159. url: url,
  160. success: res => {
  161. const {
  162. statusCode,
  163. tempFilePath
  164. } = res
  165. if (statusCode === 200) { // saveImageToPhotosAlbum saveVideoToPhotosAlbum
  166. uni.saveImageToPhotosAlbum({ //此处也可用saveVideoToPhotosAlbum
  167. filePath: tempFilePath,
  168. success: data => {
  169. console.log('data----------------->', data)
  170. uni.showToast({
  171. title: '下载成功,文件已保存到' + data.path,
  172. icon: 'success',
  173. })
  174. },
  175. complete: (msg) => {
  176. // #ifdef APP-PLUS
  177. plus.nativeUI.closeWaiting()
  178. // #endif
  179. }
  180. });
  181. } else {
  182. uni.showToast({
  183. title: '下载失败',
  184. icon: 'none',
  185. })
  186. // #ifdef APP-PLUS
  187. plus.nativeUI.closeWaiting()
  188. // #endif
  189. }
  190. },
  191. complete: () => {
  192. }
  193. })
  194. task.onProgressUpdate((res => {
  195. // console.log('上传进度' + res.progress);
  196. // console.log('已经上传的数据长度' + res.totalBytesSent);
  197. // console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
  198. showLoading.setTitle(" 正在下载" + res.progress + "% ");
  199. }))
  200. },
  201. //移动端下载文件
  202. saveFileToApp(url){
  203. var dtask = plus.downloader.createDownload(url, {},
  204. function(d, status) {
  205. uni.showToast({
  206. title: '下载完成',
  207. mask: false,
  208. duration: 1000
  209. });
  210. //console.log(dtask);
  211. // 下载完成
  212. console.log('status: ' + status);
  213. if (status == 200) {
  214. console.log('下载成功:' + d.filename);
  215. console.log('plus.io.convertLocalFileSystemURL(d.filename): ' + plus.io
  216. .convertLocalFileSystemURL(d
  217. .filename))
  218. plus.runtime.openFile(plus.io.convertLocalFileSystemURL(d.filename), {}, function(success) {
  219. console.log('打开成功')
  220. }, function(error) {
  221. console.log('打开失败')
  222. })
  223. } else {
  224. uni.showToast({
  225. title: '下载失败-02',
  226. mask: false,
  227. duration: 1500
  228. });
  229. }
  230. });
  231. try {
  232. dtask.start(); // 开启下载的任务
  233. var prg = 0;
  234. var showLoading = plus.nativeUI.showWaiting("正在下载"); //创建一个showWaiting对象
  235. dtask.addEventListener('statechanged', function(task, status) {
  236. // 给下载任务设置一个监听 并根据状态 做操作
  237. switch (task.state) {
  238. case 1:
  239. showLoading.setTitle("正在下载");
  240. break;
  241. case 2:
  242. showLoading.setTitle("已连接到服务器");
  243. break;
  244. case 3:
  245. prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);
  246. showLoading.setTitle(" 正在下载" + prg + "% ");
  247. break;
  248. case 4:
  249. plus.nativeUI.closeWaiting();
  250. //下载完成
  251. break;
  252. }
  253. });
  254. } catch (err) {
  255. plus.nativeUI.closeWaiting();
  256. uni.showToast({
  257. title: '更新失败-03',
  258. mask: false,
  259. duration: 1500
  260. });
  261. }
  262. },
  263. },
  264. watch: {
  265. fileType: {
  266. deep: true,
  267. handler: function(value) {
  268. if(this.fileUrl){
  269. this.toDownload({
  270. type:this.fileType,
  271. src:this.fileUrl,
  272. })
  273. }
  274. }
  275. }
  276. }
  277. };
  278. </script>