{"id":1315,"date":"2025-06-10T11:15:32","date_gmt":"2025-06-10T03:15:32","guid":{"rendered":"https:\/\/blog.viter.top\/?p=1315"},"modified":"2025-06-10T11:15:32","modified_gmt":"2025-06-10T03:15:32","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e5%9b%be%e7%89%87url%e4%bc%9a%e8%87%aa%e5%8a%a8%e4%b8%8b%e8%bd%bd%ef%bc%9f%e5%a6%82%e4%bd%95%e6%94%b9%e4%b8%ba%e9%a2%84%e8%a7%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/blog.viter.top\/index.php\/2025\/06\/10\/%e4%b8%ba%e4%bb%80%e4%b9%88%e5%9b%be%e7%89%87url%e4%bc%9a%e8%87%aa%e5%8a%a8%e4%b8%8b%e8%bd%bd%ef%bc%9f%e5%a6%82%e4%bd%95%e6%94%b9%e4%b8%ba%e9%a2%84%e8%a7%88%ef%bc%9f\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u56fe\u7247URL\u4f1a\u81ea\u52a8\u4e0b\u8f7d\uff1f\u5982\u4f55\u6539\u4e3a\u9884\u89c8\uff1f"},"content":{"rendered":"\n<h1><\/h1>\n\n\n\n<p>\u5f53\u4f60\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u4e00\u4e2a\u56fe\u7247\u94fe\u63a5\uff0c\u5374\u53d1\u73b0\u56fe\u7247\u81ea\u52a8\u4e0b\u8f7d\u800c\u4e0d\u662f\u76f4\u63a5\u663e\u793a\u65f6\uff0c\u8fd9\u662f\u56e0\u4e3a\u670d\u52a1\u5668\u8bbe\u7f6e\u4e86\u7279\u5b9a\u7684\u54cd\u5e94\u5934\u3002\u672c\u6587\u5c06\u7528\u7b80\u5355\u7684Node.js\u793a\u4f8b\u89e3\u91ca\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<h2 id=\"\u4e3a\u4ec0\u4e48\u4f1a\u81ea\u52a8\u4e0b\u8f7d\">\u4e3a\u4ec0\u4e48\u4f1a\u81ea\u52a8\u4e0b\u8f7d\uff1f<\/h2>\n\n\n\n<p>\u5f53\u670d\u52a1\u5668\u8fd4\u56de\u4ee5\u4e0b\u54cd\u5e94\u5934\u65f6\uff0c\u6d4f\u89c8\u5668\u4f1a\u89e6\u53d1\u4e0b\u8f7d\u884c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u9519\u8bef\u8bbe\u7f6e\uff1a\u4f1a\u5bfc\u81f4\u81ea\u52a8\u4e0b\u8f7d\nres.setHeader('Content-Type', 'application\/octet-stream');\nres.setHeader('Content-Disposition', 'attachment; filename=\"image.jpg\"');<\/code><\/pre>\n\n\n\n<p>\u539f\u56e0\u5206\u6790\uff1a<\/p>\n\n\n\n<ol><li><code>Content-Type: application\/octet-stream<\/code>&nbsp;\u544a\u8bc9\u6d4f\u89c8\u5668\u8fd9\u662f\u4e8c\u8fdb\u5236\u6570\u636e\u6d41<\/li><li><code>Content-Disposition: attachment<\/code>&nbsp;\u660e\u786e\u6307\u793a\u6d4f\u89c8\u5668\u5e94\u8be5\u4e0b\u8f7d\u6587\u4ef6<\/li><\/ol>\n\n\n\n<h2 id=\"\u5982\u4f55\u6539\u4e3a\u9884\u89c8\u6a21\u5f0f\">\u5982\u4f55\u6539\u4e3a\u9884\u89c8\u6a21\u5f0f\uff1f<\/h2>\n\n\n\n<p>\u8981\u8ba9\u56fe\u7247\u76f4\u63a5\u5728\u6d4f\u89c8\u5668\u4e2d\u663e\u793a\uff0c\u53ea\u9700\u4fee\u6539\u54cd\u5e94\u5934\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6b63\u786e\u8bbe\u7f6e\uff1a\u5b9e\u73b0\u9884\u89c8\nconst ext = path.extname(filename).toLowerCase();\nconst mimeTypes = {\n  '.jpg': 'image\/jpeg',\n  '.png': 'image\/png',\n  '.gif': 'image\/gif'\n};\n\nres.setHeader('Content-Type', mimeTypes&#91;ext] || 'image\/jpeg');\nres.setHeader('Content-Disposition', 'inline; filename=\"image.jpg\"');<\/code><\/pre>\n\n\n\n<h2 id=\"\u5b8c\u6574node-js\u793a\u4f8b\">\u5b8c\u6574Node.js\u793a\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>const http = require('http');\nconst fs = require('fs');\nconst path = require('path');\n\nconst server = http.createServer((req, res) =&gt; {\n  const filename = 'example.jpg';\n  const filePath = path.join(__dirname, filename);\n  \n  \/\/ \u8bbe\u7f6e\u9884\u89c8\u6a21\u5f0f\n  const ext = path.extname(filename).toLowerCase();\n  res.setHeader('Content-Type', {\n    '.jpg': 'image\/jpeg',\n    '.png': 'image\/png'\n  }&#91;ext] || 'image\/jpeg');\n  \n  res.setHeader('Content-Disposition', 'inline; filename=\"' + filename + '\"');\n  \n  fs.createReadStream(filePath).pipe(res);\n});\n\nserver.listen(3000);<\/code><\/pre>\n\n\n\n<h2 id=\"\u5982\u4f55\u5b9e\u73b0\u9884\u89c8-\u4e0b\u8f7d\u5207\u6362\">\u5982\u4f55\u5b9e\u73b0\u9884\u89c8\/\u4e0b\u8f7d\u5207\u6362\uff1f<\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u6839\u636e\u9700\u6c42\u5207\u6362\u9884\u89c8\u548c\u4e0b\u8f7d\u6a21\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u901a\u8fc7URL\u53c2\u6570\u63a7\u5236\u6a21\u5f0f\n\/\/ \u793a\u4f8bURL: http:\/\/localhost:3000\/image.jpg?mode=download\nconst url = new URL(req.url, `http:\/\/${req.headers.host}`);\nconst mode = url.searchParams.get('mode') || 'preview';\n\nif (mode === 'download') {\n  res.setHeader('Content-Type', 'application\/octet-stream');\n  res.setHeader('Content-Disposition', 'attachment; filename=\"' + filename + '\"');\n} else {\n  \/\/ \u9884\u89c8\u6a21\u5f0f\u4ee3\u7801...\n}<\/code><\/pre>\n\n\n\n<h2 id=\"\u603b\u7ed3\">\u603b\u7ed3<\/h2>\n\n\n\n<ol><li>\u200b<strong>\u81ea\u52a8\u4e0b\u8f7d\u539f\u56e0<\/strong>\u200b\uff1a<code>Content-Type<\/code>\u8bbe\u4e3a<code>octet-stream<\/code>&nbsp;+&nbsp;<code>Content-Disposition<\/code>\u8bbe\u4e3a<code>attachment<\/code><\/li><li>\u200b<strong>\u9884\u89c8\u89e3\u51b3\u65b9\u6848<\/strong>\u200b\uff1a\u8bbe\u7f6e\u6b63\u786e\u7684\u56fe\u7247MIME\u7c7b\u578b + \u4f7f\u7528<code>inline<\/code>\u6a21\u5f0f<\/li><li>\u200b<strong>\u7075\u6d3b\u63a7\u5236<\/strong>\u200b\uff1a\u53ef\u4ee5\u901a\u8fc7URL\u53c2\u6570\u52a8\u6001\u5207\u6362\u9884\u89c8\/\u4e0b\u8f7d\u6a21\u5f0f<\/li><\/ol>\n\n\n\n<p>\u73b0\u5728\u4f60\u53ef\u4ee5\u8f7b\u677e\u63a7\u5236\u56fe\u7247\u662f\u5728\u6d4f\u89c8\u5668\u4e2d\u663e\u793a\u8fd8\u662f\u4f5c\u4e3a\u6587\u4ef6\u4e0b\u8f7d\u4e86\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4f60\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u4e00\u4e2a\u56fe\u7247\u94fe\u63a5\uff0c\u5374\u53d1\u73b0\u56fe\u7247\u81ea\u52a8\u4e0b\u8f7d\u800c\u4e0d\u662f\u76f4\u63a5\u663e\u793a\u65f6\uff0c\u8fd9\u662f\u56e0\u4e3a\u670d\u52a1\u5668\u8bbe\u7f6e\u4e86\u7279\u5b9a\u7684\u54cd\u5e94\u5934\u3002\u672c\u6587\u5c06\u7528\u7b80\u5355\u7684Node.js &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[25],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.viter.top\/index.php\/wp-json\/wp\/v2\/posts\/1315"}],"collection":[{"href":"https:\/\/blog.viter.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.viter.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.viter.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.viter.top\/index.php\/wp-json\/wp\/v2\/comments?post=1315"}],"version-history":[{"count":1,"href":"https:\/\/blog.viter.top\/index.php\/wp-json\/wp\/v2\/posts\/1315\/revisions"}],"predecessor-version":[{"id":1316,"href":"https:\/\/blog.viter.top\/index.php\/wp-json\/wp\/v2\/posts\/1315\/revisions\/1316"}],"wp:attachment":[{"href":"https:\/\/blog.viter.top\/index.php\/wp-json\/wp\/v2\/media?parent=1315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.viter.top\/index.php\/wp-json\/wp\/v2\/categories?post=1315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.viter.top\/index.php\/wp-json\/wp\/v2\/tags?post=1315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}