{"id":3463,"date":"2024-11-10T08:46:13","date_gmt":"2024-11-10T00:46:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3463\/"},"modified":"2024-11-10T08:46:13","modified_gmt":"2024-11-10T00:46:13","slug":"js-%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96blob","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3463\/","title":{"rendered":"js \u5982\u4f55\u83b7\u53d6blob"},"content":{"rendered":"<blockquote><p>\n  javascript \u4e2d\u83b7\u53d6 blob \u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u521b\u5efa\u65b0 blob\u3002\u901a\u8fc7\u6587\u4ef6\u8f93\u5165\u8bfb\u53d6\u6587\u4ef6\u3002\u4ece xmlhttprequest \u54cd\u5e94\u4e2d\u63d0\u53d6 blobs\u3002\u901a\u8fc7 fetch api \u4ece\u7f51\u7edc\u83b7\u53d6 blobs\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110812542654958.jpg\" class=\"aligncenter\" title=\"js \u5982\u4f55\u83b7\u53d6blob\u63d2\u56fe\" alt=\"js \u5982\u4f55\u83b7\u53d6blob\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u83b7\u53d6 Blob<\/strong><\/p>\n<p><strong>\u5728 JavaScript \u4e2d\u83b7\u53d6 Blob \u7684\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u53ef\u7528\u4e8e\u5728 JavaScript \u4e2d\u83b7\u53d6 Blob\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u65b0\u7684 Blob\uff1a<\/strong><\/p>\n<pre>const myBlob = new Blob(['Hello World!'], {type: 'text\/plain'});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u901a\u8fc7\u6587\u4ef6\u8f93\u5165\u8bfb\u53d6\u6587\u4ef6\uff1a<\/strong><\/p>\n<pre>const fileInput = document.getElementById('myFile');\n\nfileInput.addEventListener('change', (e) =&gt; {\n  const file = e.target.files[0];\n  const reader = new FileReader();\n\n  reader.onload = () =&gt; {\n    const blob = new Blob([reader.result], {type: file.type});\n    \/\/ ...\n  };\n\n  reader.readAsArrayBuffer(file);\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u4ece XMLHttpRequest \u54cd\u5e94\u4e2d\u63d0\u53d6 Blobs\uff1a<\/strong><\/p>\n<pre>const xhr = new XMLHttpRequest();\n\nxhr.open('GET', 'my-file.txt');\nxhr.responseType = 'blob';\n\nxhr.onload = () =&gt; {\n  const blob = xhr.response;\n  \/\/ ...\n};\n\nxhr.send();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u901a\u8fc7 Fetch API \u4ece\u7f51\u7edc\u83b7\u53d6 Blobs\uff1a<\/strong><\/p>\n<pre>fetch('my-file.txt', {\n  method: 'GET',\n  responseType: 'blob'\n})\n.then((response) =&gt; {\n  const blob = response.blob();\n  \/\/ ...\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>Blob \u5bf9\u8c61\u7684\u7528\u6cd5\uff1a<\/strong><\/p>\n<p>Blob \u5bf9\u8c61\u8868\u793a\u4e0d\u53ef\u53d8\u7684\u539f\u59cb\u6570\u636e\uff0c\u901a\u5e38\u5305\u542b\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4f8b\u5982\u56fe\u50cf\u3001\u97f3\u9891\u6216\u89c6\u9891\u6587\u4ef6\u3002\u5b83\u63d0\u4f9b\u7528\u4e8e\u64cd\u4f5c\u548c\u8bfb\u53d6 Blob \u5185\u5bb9\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>size\uff1a\u83b7\u53d6 Blob \u7684\u5927\u5c0f\u3002<\/li>\n<li>type\uff1a\u83b7\u53d6 Blob \u7684 MIME \u7c7b\u578b\u3002<\/li>\n<li>slice(start, end)\uff1a\u5c06 Blob \u7684\u6307\u5b9a\u90e8\u5206\u4f5c\u4e3a\u65b0 Blob \u8fd4\u56de\u3002<\/li>\n<li>arrayBuffer()\uff1a\u5c06 Blob \u8f6c\u6362\u4e3a ArrayBuffer\u3002<\/li>\n<li>text()\uff1a\u5c06 Blob \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs \u5982\u4f55\u83b7\u53d6blob\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u4e2d\u83b7\u53d6 blob \u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u521b\u5efa\u65b0 blob\u3002\u901a\u8fc7\u6587\u4ef6\u8f93\u5165\u8bfb\u53d6\u6587\u4ef6\u3002\u4ece xmlhttprequest \u54cd\u5e94\u4e2d\u63d0\u53d6 blobs\u3002\u901a\u8fc7 fetch api \u4ece\u7f51\u7edc\u83b7\u53d6 blobs\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u83b7\u53d6 Blob \u5728 JavaScript \u4e2d\u83b7\u53d6 Blob \u7684\u65b9\u6cd5\uff1a \u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u53ef\u7528\u4e8e\u5728 JavaScript \u4e2d\u83b7\u53d6 Blob\uff1a 1. \u521b\u5efa\u65b0\u7684 Blob\uff1a const myBlob = new Blob([&#8216;Hello World!&#8217;], {type: &#8216;text\/plain&#8217;}); \u767b\u5f55\u540e\u590d\u5236 2. \u901a\u8fc7\u6587\u4ef6\u8f93\u5165\u8bfb\u53d6\u6587\u4ef6\uff1a const fileInput = document.getElementById(&#8216;myFile&#8217;); fileInput.addEventListener(&#8216;change&#8217;, (e) =&gt; { const file = e.target.files[0]; const reader = new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3463","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3463"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3463\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}