翼度科技»论坛 编程开发 JavaScript 查看内容

前端使用JS内置Blob实现下载各种形式的文件实例

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
axios调用后端下载接口(在请求体中加上responseType: "blob")
  1. export const fileDownload = (data) =>
  2.   request({
  3.     method: "post",
  4.     url: "/heritage/fileDownload",
  5.     responseType: "blob",
  6.     data,
  7.   });
复制代码
把后端返回的二进制流数据转换成一个blob对象,再去进行下载;
1.封装下载函数
  1. // data: 接口返回的二进制流数据
  2. // fileName:自定义下载完成后的文件名
  3. // fileType:要下载的文件类型

  4. downloadFile(data, fileName, fileType) {
  5.   let blob = new Blob([data], { type: `${fileType};charset=utf-8`});// type指定下载文件的类型
  6.   let downloadElement = document.createElement("a");
  7.   let href = window.URL.createObjectURL(blob);
  8.   downloadElement.href = href;
  9.   downloadElement.download = fileName;
  10.   document.body.appendChild(downloadElement);
  11.   downloadElement.click();
  12.   document.body.removeChild(downloadElement);
  13.   window.URL.revokeObjectURL(href);
  14. }
复制代码
2. 当后端下载接口请求完成后,调用1中的下载函数并传入对应参数;
  1. async courseDownload() {
  2.   // 调用后端接口
  3.   const res = await fileDownload();

  4.   // 此处的fileType可以动态获取响应头中对应的文件名称
  5.   const filename = "测试文件"; // 自定义下载文件名称

  6.   // 此处的fileType可以动态获取响应头中对应的文件类型
  7.   const fileType = "application/zip" // 下载zip格式的文件
  8.   
  9.   this.downloadFile(res, filename, fileType);
  10. },
复制代码
new Blob的参数type类型:
  1. //作者:夹板儿孩
  2. //链接:https://www.jianshu.com/p/6896ade6299b
  3. // 全面的类型:https://www.iana.org/assignments/media-types/media-types.xhtml
  4. 123 application/vnd.lotus-1-2-3
  5. 3gp video/3gpp
  6. aab application/x-authoware-bin
  7. aam application/x-authoware-map
  8. aas application/x-authoware-seg
  9. ai application/postscript
  10. aif audio/x-aiff
  11. aifc audio/x-aiff
  12. aiff audio/x-aiff
  13. als audio/X-Alpha5
  14. amc application/x-mpeg
  15. ani application/octet-stream
  16. asc text/plain
  17. asd application/astound
  18. asf video/x-ms-asf
  19. asn application/astound
  20. asp application/x-asap
  21. asx video/x-ms-asf
  22. au audio/basic
  23. avb application/octet-stream
  24. avi video/x-msvideo
  25. awb audio/amr-wb
  26. bcpio application/x-bcpio
  27. bin application/octet-stream
  28. bld application/bld
  29. bld2 application/bld2
  30. bmp application/x-MS-bmp
  31. bpk application/octet-stream
  32. bz2 application/x-bzip2
  33. cal image/x-cals
  34. ccn application/x-cnc
  35. cco application/x-cocoa
  36. cdf application/x-netcdf
  37. cgi magnus-internal/cgi
  38. chat application/x-chat
  39. class application/octet-stream
  40. clp application/x-msclip
  41. cmx application/x-cmx
  42. co application/x-cult3d-object
  43. cod image/cis-cod
  44. cpio application/x-cpio
  45. cpt application/mac-compactpro
  46. crd application/x-mscardfile
  47. csh application/x-csh
  48. csm chemical/x-csml
  49. csml chemical/x-csml
  50. css text/css
  51. cur application/octet-stream
  52. dcm x-lml/x-evm
  53. dcr application/x-director
  54. dcx image/x-dcx
  55. dhtml text/html
  56. dir application/x-director
  57. dll application/octet-stream
  58. dmg application/octet-stream
  59. dms application/octet-stream
  60. doc application/msword
  61. dot application/x-dot
  62. dvi application/x-dvi
  63. dwf drawing/x-dwf
  64. dwg application/x-autocad
  65. dxf application/x-autocad
  66. dxr application/x-director
  67. ebk application/x-expandedbook
  68. emb chemical/x-embl-dl-nucleotide
  69. embl chemical/x-embl-dl-nucleotide
  70. eps application/postscript
  71. eri image/x-eri
  72. es audio/echospeech
  73. esl audio/echospeech
  74. etc application/x-earthtime
  75. etx text/x-setext
  76. evm x-lml/x-evm
  77. evy application/x-envoy
  78. exe application/octet-stream
  79. fh4 image/x-freehand
  80. fh5 image/x-freehand
  81. fhc image/x-freehand
  82. fif image/fif
  83. fm application/x-maker
  84. fpx image/x-fpx
  85. fvi video/isivideo
  86. gau chemical/x-gaussian-input
  87. gca application/x-gca-compressed
  88. gdb x-lml/x-gdb
  89. gif image/gif
  90. gps application/x-gps
  91. gtar application/x-gtar
  92. gz application/x-gzip
  93. hdf application/x-hdf
  94. hdm text/x-hdml
  95. hdml text/x-hdml
  96. hlp application/winhlp
  97. hqx application/mac-binhex40
  98. htm text/html
  99. html text/html
  100. hts text/html
  101. ice x-conference/x-cooltalk
  102. ico application/octet-stream
  103. ief image/ief
  104. ifm image/gif
  105. ifs image/ifs
  106. imy audio/melody
  107. ins application/x-NET-Install
  108. ips application/x-ipscript
  109. ipx application/x-ipix
  110. it audio/x-mod
  111. itz audio/x-mod
  112. ivr i-world/i-vrml
  113. j2k image/j2k
  114. jad text/vnd.sun.j2me.app-descriptor
  115. jam application/x-jam
  116. jar application/java-archive
  117. jnlp application/x-java-jnlp-file
  118. jpe image/jpeg
  119. jpeg image/jpeg
  120. jpg image/jpeg
  121. jpz image/jpeg
  122. js application/x-javascript
  123. jwc application/jwc
  124. kjx application/x-kjx
  125. lak x-lml/x-lak
  126. latex application/x-latex
  127. lcc application/fastman
  128. lcl application/x-digitalloca
  129. lcr application/x-digitalloca
  130. lgh application/lgh
  131. lha application/octet-stream
  132. lml x-lml/x-lml
  133. lmlpack x-lml/x-lmlpack
  134. lsf video/x-ms-asf
  135. lsx video/x-ms-asf
  136. lzh application/x-lzh
  137. m13 application/x-msmediaview
  138. m14 application/x-msmediaview
  139. m15 audio/x-mod
  140. m3u audio/x-mpegurl
  141. m3url audio/x-mpegurl
  142. ma1 audio/ma1
  143. ma2 audio/ma2
  144. ma3 audio/ma3
  145. ma5 audio/ma5
  146. man application/x-troff-man
  147. map magnus-internal/imagemap
  148. mbd application/mbedlet
  149. mct application/x-mascot
  150. mdb application/x-msaccess
  151. mdz audio/x-mod
  152. me application/x-troff-me
  153. mel text/x-vmel
  154. mi application/x-mif
  155. mid audio/midi
  156. midi audio/midi
  157. mif application/x-mif
  158. mil image/x-cals
  159. mio audio/x-mio
  160. mmf application/x-skt-lbs
  161. mng video/x-mng
  162. mny application/x-msmoney
  163. moc application/x-mocha
  164. mocha application/x-mocha
  165. mod audio/x-mod
  166. mof application/x-yumekara
  167. mol chemical/x-mdl-molfile
  168. mop chemical/x-mopac-input
  169. mov video/quicktime
  170. movie video/x-sgi-movie
  171. mp2 audio/x-mpeg
  172. mp3 audio/x-mpeg
  173. mp4 video/mp4
  174. mpc application/vnd.mpohun.certificate
  175. mpe video/mpeg
  176. mpeg video/mpeg
  177. mpg video/mpeg
  178. mpg4 video/mp4
  179. mpga audio/mpeg
  180. mpn application/vnd.mophun.application
  181. mpp application/vnd.ms-project
  182. mps application/x-mapserver
  183. mrl text/x-mrml
  184. mrm application/x-mrm
  185. ms application/x-troff-ms
  186. mts application/metastream
  187. mtx application/metastream
  188. mtz application/metastream
  189. mzv application/metastream
  190. nar application/zip
  191. nbmp image/nbmp
  192. nc application/x-netcdf
  193. ndb x-lml/x-ndb
  194. ndwn application/ndwn
  195. nif application/x-nif
  196. nmz application/x-scream
  197. nokia-op-logo image/vnd.nok-oplogo-color
  198. npx application/x-netfpx
  199. nsnd audio/nsnd
  200. nva application/x-neva1
  201. oda application/oda
  202. oom application/x-AtlasMate-Plugin
  203. pac audio/x-pac
  204. pae audio/x-epac
  205. pan application/x-pan
  206. pbm image/x-portable-bitmap
  207. pcx image/x-pcx
  208. pda image/x-pda
  209. pdb chemical/x-pdb
  210. pdf application/pdf
  211. pfr application/font-tdpfr
  212. pgm image/x-portable-graymap
  213. pict image/x-pict
  214. pm application/x-perl
  215. pmd application/x-pmd
  216. png image/png
  217. pnm image/x-portable-anymap
  218. pnz image/png
  219. pot application/vnd.ms-powerpoint
  220. ppm image/x-portable-pixmap
  221. pps application/vnd.ms-powerpoint
  222. ppt application/vnd.ms-powerpoint
  223. pqf application/x-cprplayer
  224. pqi application/cprplayer
  225. prc application/x-prc
  226. proxy application/x-ns-proxy-autoconfig
  227. ps application/postscript
  228. ptlk application/listenup
  229. pub application/x-mspublisher
  230. pvx video/x-pv-pvx
  231. qcp audio/vnd.qcelp
  232. qt video/quicktime
  233. qti image/x-quicktime
  234. qtif image/x-quicktime
  235. r3t text/vnd.rn-realtext3d
  236. ra audio/x-pn-realaudio
  237. ram audio/x-pn-realaudio
  238. rar application/x-rar-compressed
  239. ras image/x-cmu-raster
  240. rdf application/rdf+xml
  241. rf image/vnd.rn-realflash
  242. rgb image/x-rgb
  243. rlf application/x-richlink
  244. rm audio/x-pn-realaudio
  245. rmf audio/x-rmf
  246. rmm audio/x-pn-realaudio
  247. rmvb audio/x-pn-realaudio
  248. rnx application/vnd.rn-realplayer
  249. roff application/x-troff
  250. rp image/vnd.rn-realpix
  251. rpm audio/x-pn-realaudio-plugin
  252. rt text/vnd.rn-realtext
  253. rte x-lml/x-gps
  254. rtf application/rtf
  255. rtg application/metastream
  256. rtx text/richtext
  257. rv video/vnd.rn-realvideo
  258. rwc application/x-rogerwilco
  259. s3m audio/x-mod
  260. s3z audio/x-mod
  261. sca application/x-supercard
  262. scd application/x-msschedule
  263. sdf application/e-score
  264. sea application/x-stuffit
  265. sgm text/x-sgml
  266. sgml text/x-sgml
  267. sh application/x-sh
  268. shar application/x-shar
  269. shtml magnus-internal/parsed-html
  270. shw application/presentations
  271. si6 image/si6
  272. si7 image/vnd.stiwap.sis
  273. si9 image/vnd.lgtwap.sis
  274. sis application/vnd.symbian.install
  275. sit application/x-stuffit
  276. skd application/x-Koan
  277. skm application/x-Koan
  278. skp application/x-Koan
  279. skt application/x-Koan
  280. slc application/x-salsa
  281. smd audio/x-smd
  282. smi application/smil
  283. smil application/smil
  284. smp application/studiom
  285. smz audio/x-smd
  286. snd audio/basic
  287. spc text/x-speech
  288. spl application/futuresplash
  289. spr application/x-sprite
  290. sprite application/x-sprite
  291. spt application/x-spt
  292. src application/x-wais-source
  293. stk application/hyperstudio
  294. stm audio/x-mod
  295. sv4cpio application/x-sv4cpio
  296. sv4crc application/x-sv4crc
  297. svf image/vnd
  298. svg image/svg-xml
  299. svh image/svh
  300. svr x-world/x-svr
  301. swf application/x-shockwave-flash
  302. swfl application/x-shockwave-flash
  303. t application/x-troff
  304. tad application/octet-stream
  305. talk text/x-speech
  306. tar application/x-tar
  307. taz application/x-tar
  308. tbp application/x-timbuktu
  309. tbt application/x-timbuktu
  310. tcl application/x-tcl
  311. tex application/x-tex
  312. texi application/x-texinfo
  313. texinfo application/x-texinfo
  314. tgz application/x-tar
  315. thm application/vnd.eri.thm
  316. tif image/tiff
  317. tiff image/tiff
  318. tki application/x-tkined
  319. tkined application/x-tkined
  320. toc application/toc
  321. toy image/toy
  322. tr application/x-troff
  323. trk x-lml/x-gps
  324. trm application/x-msterminal
  325. tsi audio/tsplayer
  326. tsp application/dsptype
  327. tsv text/tab-separated-values
  328. tsv text/tab-separated-values
  329. ttf application/octet-stream
  330. ttz application/t-time
  331. txt text/plain
  332. ult audio/x-mod
  333. ustar application/x-ustar
  334. uu application/x-uuencode
  335. uue application/x-uuencode
  336. vcd application/x-cdlink
  337. vcf text/x-vcard
  338. vdo video/vdo
  339. vib audio/vib
  340. viv video/vivo
  341. vivo video/vivo
  342. vmd application/vocaltec-media-desc
  343. vmf application/vocaltec-media-file
  344. vmi application/x-dreamcast-vms-info
  345. vms application/x-dreamcast-vms
  346. vox audio/voxware
  347. vqe audio/x-twinvq-plugin
  348. vqf audio/x-twinvq
  349. vql audio/x-twinvq
  350. vre x-world/x-vream
  351. vrml x-world/x-vrml
  352. vrt x-world/x-vrt
  353. vrw x-world/x-vream
  354. vts workbook/formulaone
  355. wav audio/x-wav
  356. wax audio/x-ms-wax
  357. wbmp image/vnd.wap.wbmp
  358. web application/vnd.xara
  359. wi image/wavelet
  360. wis application/x-InstallShield
  361. wm video/x-ms-wm
  362. wma audio/x-ms-wma
  363. wmd application/x-ms-wmd
  364. wmf application/x-msmetafile
  365. wml text/vnd.wap.wml
  366. wmlc application/vnd.wap.wmlc
  367. wmls text/vnd.wap.wmlscript
  368. wmlsc application/vnd.wap.wmlscriptc
  369. wmlscript text/vnd.wap.wmlscript
  370. wmv audio/x-ms-wmv
  371. wmx video/x-ms-wmx
  372. wmz application/x-ms-wmz
  373. wpng image/x-up-wpng
  374. wpt x-lml/x-gps
  375. wri application/x-mswrite
  376. wrl x-world/x-vrml
  377. wrz x-world/x-vrml
  378. ws text/vnd.wap.wmlscript
  379. wsc application/vnd.wap.wmlscriptc
  380. wv video/wavelet
  381. wvx video/x-ms-wvx
  382. wxl application/x-wxl
  383. x-gzip application/x-gzip
  384. xar application/vnd.xara
  385. xbm image/x-xbitmap
  386. xdm application/x-xdma
  387. xdma application/x-xdma
  388. xdw application/vnd.fujixerox.docuworks
  389. xht application/xhtml+xml
  390. xhtm application/xhtml+xml
  391. xhtml application/xhtml+xml
  392. xla application/vnd.ms-excel
  393. xlc application/vnd.ms-excel
  394. xll application/x-excel
  395. xlm application/vnd.ms-excel
  396. xls application/vnd.ms-excel
  397. xlt application/vnd.ms-excel
  398. xlw application/vnd.ms-excel
  399. xm audio/x-mod
  400. xml text/xml
  401. xmz audio/x-mod
  402. xpi application/x-xpinstall
  403. xpm image/x-xpixmap
  404. xsit text/xml
  405. xsl text/xml
  406. xul text/xul
  407. xwd image/x-xwindowdump
  408. xyz chemical/x-pdb
  409. yz1 application/x-yz1
  410. z application/x-compress
  411. zac application/x-zaurus-zac
  412. zip application/zip
  413. ogg application/ogg
  414. aac         audio/aac
复制代码
总结
到此这篇关于前端使用JS内置Blob实现下载各种形式文件的文章就介绍到这了,更多相关JS内置Blob下载文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/javascript/32338834h.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具