{"id":520,"date":"2025-12-05T19:30:34","date_gmt":"2025-12-05T11:30:34","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=520"},"modified":"2025-12-05T19:30:34","modified_gmt":"2025-12-05T11:30:34","slug":"javascript%e4%b8%adurl-createobjecturl%e5%92%8cblob%e5%ae%9e%e7%8e%b0%e4%bf%9d%e5%ad%98%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/javascript%e4%b8%adurl-createobjecturl%e5%92%8cblob%e5%ae%9e%e7%8e%b0%e4%bf%9d%e5%ad%98%e6%96%87%e4%bb%b6\/","title":{"rendered":"JavaScript\u4e2dURL.createObjectURL\u548cBlob\u5b9e\u73b0\u4fdd\u5b58\u6587\u4ef6"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>\u524d\u7aef\u5b9e\u6218\u7ea7\u3001\u53ef\u590d\u5236\u5373\u53ef\u7528\u7684<\/strong><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u300aJavaScript \u4e2d URL.createObjectURL + Blob \u5b9e\u73b0\u4fdd\u5b58\u6587\u4ef6\uff08\u8d85\u8be6\u7ec6\u6559\u7a0b\uff09\u300b<\/h1>\n\n\n\n<p>\u6db5\u76d6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u539f\u7406<\/li>\n\n\n\n<li>\u56fe\u7247 \/ \u6587\u672c \/ JSON \/ \u6587\u4ef6\u4e0b\u8f7d<\/li>\n\n\n\n<li>\u524d\u7aef\u5bfc\u51fa\u6587\u4ef6\u5b8c\u6574\u793a\u4f8b<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e00\u3001\u6838\u5fc3\u6982\u5ff5\uff1aBlob \u4e0e createObjectURL<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>Blob \u662f\u4ec0\u4e48\uff1f<\/strong><\/h2>\n\n\n\n<p>Blob\uff08Binary Large Object\uff09\u8868\u793a <strong>\u4e8c\u8fdb\u5236\u6570\u636e\u5757<\/strong>\uff0c\u5e38\u7528\u4e8e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6587\u4ef6\u4e0b\u8f7d<\/li>\n\n\n\n<li>\u56fe\u7247\u9884\u89c8<\/li>\n\n\n\n<li>\u4e0a\u4f20\u6587\u4ef6<\/li>\n\n\n\n<li>\u97f3\u89c6\u9891\u5904\u7406<\/li>\n<\/ul>\n\n\n\n<p>\u7b80\u5355\u7406\u89e3\uff1a<\/p>\n\n\n\n<p>\ud83d\udc49 <strong>Blob = \u524d\u7aef\u6784\u9020\u7684\u201c\u6587\u4ef6\u201d<\/strong>\u3002<\/p>\n\n\n\n<p>\u521b\u5efa\u65b9\u5f0f\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst blob = new Blob(&#x5B;&quot;hello world&quot;], { type: &quot;text\/plain&quot; });\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>URL.createObjectURL() \u662f\u4ec0\u4e48\uff1f<\/strong><\/h2>\n\n\n\n<p>\u5b83\u7528\u4e8e\u4e3a <code>Blob<\/code> \u6216 <code>File<\/code> \u521b\u5efa\u4e00\u4e2a <strong>\u4e34\u65f6 URL<\/strong>\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst url = URL.createObjectURL(blob);\n\n<\/pre><\/div>\n\n\n<p>\u751f\u6210\u7684 URL \u7c7b\u4f3c\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nblob:https:\/\/xxx.com\/df7a9sdf-9234...\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u79cd URL \u53ef\u4ee5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u89e6\u53d1\u4e0b\u8f7d<\/li>\n\n\n\n<li>\u653e\u5165 <code>&lt;img><\/code> \u663e\u793a<\/li>\n\n\n\n<li><code>&lt;video><\/code> \u64ad\u653e<\/li>\n<\/ul>\n\n\n\n<p>\u5e76\u4e14\u65e0\u9700\u4e0a\u4f20\u5230\u670d\u52a1\u5668\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\ude80 \u4e8c\u3001\u4f7f\u7528 Blob + createObjectURL \u4e0b\u8f7d\u6587\u4ef6\uff08\u901a\u7528\u6a21\u677f\uff09<\/h1>\n\n\n\n<p>\u8fd9\u662f\u4f60\u524d\u7aef\u4e0b\u8f7d\u6587\u4ef6\u6700\u5e38\u7528\u7684\u4ee3\u7801\uff0c\u76f4\u63a5\u53ef\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction downloadBlob(blob, filename) {\n  const url = URL.createObjectURL(blob);\n\n  const a = document.createElement(&quot;a&quot;);\n  a.href = url;\n  a.download = filename;\n\n  \/\/ \u89e6\u53d1\u4e0b\u8f7d\n  a.click();\n\n  \/\/ \u91ca\u653e URL\n  URL.revokeObjectURL(url);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e09\u3001\u4e0b\u8f7d\u4e0d\u540c\u7c7b\u578b\u6587\u4ef6\u7684\u5b9e\u6218\u793a\u4f8b<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2460 \u4e0b\u8f7d TXT \u6587\u672c\u6587\u4ef6<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst blob = new Blob(&#x5B;&quot;\u8fd9\u662f\u4e00\u4e2a\u6587\u672c\u6587\u4ef6&quot;], {\n  type: &quot;text\/plain;charset=utf-8&quot;\n});\n\ndownloadBlob(blob, &quot;test.txt&quot;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2461 \u4e0b\u8f7d JSON \u6587\u4ef6<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst data = { name: &quot;\u963f\u6770&quot;, age: 20 };\n\nconst blob = new Blob(&#x5B;JSON.stringify(data, null, 2)], {\n  type: &quot;application\/json&quot;\n});\n\ndownloadBlob(blob, &quot;data.json&quot;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2462 \u4e0b\u8f7d\u56fe\u7247\uff08\u5c06 base64 \u8f6c\u6362\u4e3a Blob\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction base64ToBlob(base64, contentType = &quot;image\/png&quot;) {\n  const byteString = atob(base64.split(&quot;,&quot;)&#x5B;1]);\n  const buffer = new ArrayBuffer(byteString.length);\n  const uint8 = new Uint8Array(buffer);\n\n  for (let i = 0; i &amp;lt; byteString.length; i++) {\n    uint8&#x5B;i] = byteString.charCodeAt(i);\n  }\n\n  return new Blob(&#x5B;buffer], { type: contentType });\n}\n\nconst blob = base64ToBlob(base64String);\ndownloadBlob(blob, &quot;image.png&quot;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2463 \u4e0b\u8f7d\u540e\u7aef\u8fd4\u56de\u7684\u6587\u4ef6\u6d41\uff08arraybuffer\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfetch(&quot;\/api\/download&quot;)\n  .then(res =&gt; res.arrayBuffer())\n  .then(buffer =&gt; {\n    const blob = new Blob(&#x5B;buffer], {\n      type: &quot;application\/octet-stream&quot;\n    });\n    downloadBlob(blob, &quot;file.zip&quot;);\n  });\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2464 \u4e0b\u8f7d CSV \u6587\u4ef6<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst csvContent = &quot;name,age\\n\u963f\u6770,18\\n\u5c0f\u738b,20&quot;;\n\nconst blob = new Blob(&#x5B;csvContent], {\n  type: &quot;text\/csv;charset=utf-8;&quot;\n});\n\ndownloadBlob(blob, &quot;users.csv&quot;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2465 \u4f7f\u7528 createObjectURL \u663e\u793a\u56fe\u7247\uff08\u975e\u4e0b\u8f7d\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst blob = new Blob(&#x5B;file], { type: file.type });\nconst url = URL.createObjectURL(blob);\n\ndocument.getElementById(&quot;preview&quot;).src = url;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde8 \u56db\u3001\u5173\u4e8e URL.revokeObjectURL() \u7684\u91cd\u8981\u8bf4\u660e<\/h1>\n\n\n\n<p>\u4e00\u5b9a\u8981\u8bb0\u5f97\u91ca\u653e\uff01<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nURL.revokeObjectURL(url);\n\n<\/pre><\/div>\n\n\n<p>\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5185\u5b58\u6cc4\u6f0f<\/li>\n\n\n\n<li>\u5bf9\u8c61 URL \u65e0\u4e0a\u9650\u7d2f\u79ef<\/li>\n<\/ul>\n\n\n\n<p>\u6d4f\u89c8\u5668\u4e0d\u4f1a\u81ea\u52a8\u56de\u6536\u8fd9\u4e9b URL\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udee0 \u4e94\u3001\u5c01\u88c5\u4e00\u4e2a\u4e07\u80fd\u201c\u524d\u7aef\u4fdd\u5b58\u6587\u4ef6\u201d\u5de5\u5177\uff08\u53ef\u653e\u5165 utils\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport function saveFile(content, filename, mimeType) {\n  const blob = new Blob(&#x5B;content], { type: mimeType });\n  const url = URL.createObjectURL(blob);\n\n  const a = document.createElement(&quot;a&quot;);\n  a.href = url;\n  a.download = filename;\n  a.style.display = &quot;none&quot;;\n\n  document.body.appendChild(a);\n  a.click();\n  document.body.removeChild(a);\n\n  URL.revokeObjectURL(url);\n}\n\n<\/pre><\/div>\n\n\n<p>\u4f7f\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsaveFile(&quot;hello world&quot;, &quot;hello.txt&quot;, &quot;text\/plain&quot;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udccc \u516d\u3001Vue \u4e2d\u5982\u4f55\u4f7f\u7528\uff1f<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;script setup&gt;\nimport { saveFile } from &quot;@\/utils\/saveFile.js&quot;;\n\nconst download = () =&gt; {\n  saveFile(&quot;Vue 3 \u5f88\u597d\u7528\uff01&quot;, &quot;vue.txt&quot;, &quot;text\/plain&quot;);\n};\n&amp;lt;\/script&gt;\n\n&amp;lt;template&gt;\n  &amp;lt;button @click=&quot;download&quot;&gt;\u4e0b\u8f7d\u6587\u4ef6&amp;lt;\/button&gt;\n&amp;lt;\/template&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf \u4e03\u3001\u7ec8\u6781\u603b\u7ed3\uff08\u80cc\u4e0b\u6765\u5c31\u4f1a\u7528\uff09<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>API<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td>Blob<\/td><td>\u524d\u7aef\u751f\u6210\u6587\u4ef6<\/td><\/tr><tr><td>URL.createObjectURL<\/td><td>\u751f\u6210\u4e34\u65f6\u6587\u4ef6 URL<\/td><\/tr><tr><td>a.download<\/td><td>\u6d4f\u89c8\u5668\u89e6\u53d1\u4e0b\u8f7d<\/td><\/tr><tr><td>URL.revokeObjectURL<\/td><td>\u91ca\u653e\u5185\u5b58<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd \u524d\u7aef\u5b9e\u6218\u7ea7\u3001\u53ef\u590d\u5236\u5373\u53ef\u7528\u7684 \u300aJavaScript \u4e2d URL.c&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/javascript%e4%b8%adurl-createobjecturl%e5%92%8cblob%e5%ae%9e%e7%8e%b0%e4%bf%9d%e5%ad%98%e6%96%87%e4%bb%b6\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-520","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=520"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/520\/revisions"}],"predecessor-version":[{"id":521,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/520\/revisions\/521"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}