nodejs读取图片返回给浏览器显示
Others
2020-08-26 07:34:22
2020-08-26 07:34:22
1665 次浏览
请求头说明
在http响应里面有几个重要的东西,Content-Type 说明文件渲染MIME类型,这是我们本文的相关处理关键。
常用的MIME类型
{
"css": "text/css",
"gif": "image/gif",
"html": "text/html",
"ico": "image/x-icon",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"pdf": "application/pdf",
"png": "image/png",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tiff": "image/tiff",
"txt": "text/plain",
"wav": "audio/x-wav",
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"xml": "text/xml" }
<li style="box-sizing:border-box;outline:0px;margin:0px;padding:0px 8px;list-style:none;font-size:inherit;overflow-wrap:break-word;color:#999999;">
</li>
处理方法
一、静态返回资源
原理:
使用node.js的fs.readFile来处理。根据请求地址,转换成实际的文件地址。判断文件是否存在,不存在返回404,存在则读取文件 ,并返回文件
<div style="color:#333333;background-color:#F5F5F5;font-family:Consolas, "font-size:14px;line-height:19px;white-space:pre;">
<div>
<span style="color:#7a3e9d;">response</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">setHeader</span>(<span style="color:#777777;">"</span><span style="color:#448c27;">Content-Type</span><span style="color:#777777;">"</span><span style="color:#777777;">,</span> <span style="color:#7a3e9d;">obj</span><span style="color:#777777;">.</span><span style="color:#7a3e9d;">jpg</span>)<span style="color:#777777;">;</span> <span style="color:#aaaaaa;font-style:italic;">//格式必须为 binary 否则会出错</span>
</div>
<div>
<span style="color:#7a3e9d;">var</span> <span style="color:#7a3e9d;">content</span> <span style="color:#777777;">=</span> <span style="color:#7a3e9d;">fs</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">readFileSync</span>(<span style="color:#7a3e9d;">url</span><span style="color:#777777;">,</span><span style="color:#777777;">"</span><span style="color:#448c27;">binary</span><span style="color:#777777;">"</span>)<span style="color:#777777;">;</span>
</div>
<div>
<span style="color:#7a3e9d;">response</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">writeHead</span>(<span style="color:#9c5d27;">200</span><span style="color:#777777;">,</span> <span style="color:#777777;">"</span><span style="color:#448c27;">Ok</span><span style="color:#777777;">"</span>)<span style="color:#777777;">;</span>
</div>
<div>
<span style="color:#7a3e9d;">response</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">write</span>(<span style="color:#7a3e9d;">content</span><span style="color:#777777;">,</span><span style="color:#777777;">"</span><span style="color:#448c27;">binary</span><span style="color:#777777;">"</span>)<span style="color:#777777;">;</span> <span style="color:#aaaaaa;font-style:italic;">//格式必须为 binary,否则会出错 </span>
</div>
<div>
<span style="color:#7a3e9d;">response</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">end</span>()<span style="color:#777777;">;</span>
</div>
</div>
<li style="box-sizing:border-box;outline:0px;margin:0px;padding:0px 8px;list-style:none;font-size:inherit;overflow-wrap:break-word;color:#999999;">
</li>
二、动态文件流处理
原理:
使用nodejs 的fs.createReadStream来处理,这样处理的好处是断点续传。
<div style="color:#333333;background-color:#F5F5F5;font-family:Consolas, "font-size:14px;line-height:19px;white-space:pre;">
<div>
<span style="color:#7a3e9d;">response</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">set</span>( <span style="color:#777777;">'</span><span style="color:#448c27;">content-type</span><span style="color:#777777;">'</span><span style="color:#777777;">,</span> <span style="color:#7a3e9d;">obj</span><span style="color:#777777;">.</span><span style="color:#7a3e9d;">jpg</span> )<span style="color:#777777;">;</span>
</div>
<div>
<span style="color:#7a3e9d;">var</span> <span style="color:#7a3e9d;">stream</span> <span style="color:#777777;">=</span> <span style="color:#7a3e9d;">fs</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">createReadStream</span>( <span style="color:#7a3e9d;">url</span> )<span style="color:#777777;">;</span>
</div>
<div>
<span style="color:#7a3e9d;">var</span> <span style="color:#7a3e9d;">responseData</span> <span style="color:#777777;">=</span> []<span style="color:#777777;">;</span>
</div>
<div>
<span style="color:#4b69c6;">if</span> (<span style="color:#7a3e9d;">stream</span>) <span style="color:#777777;">{</span>
</div>
<div>
<span style="color:#7a3e9d;">stream</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">on</span>( <span style="color:#777777;">'</span><span style="color:#448c27;">data</span><span style="color:#777777;">'</span><span style="color:#777777;">,</span> <span style="color:#7a3e9d;">function</span><span style="color:#777777;">(</span> <span style="color:#7a3e9d;">chunk</span> <span style="color:#777777;">)</span> <span style="color:#777777;">{</span> <span style="color:#7a3e9d;">responseData</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">push</span>( <span style="color:#7a3e9d;">chunk</span> )<span style="color:#777777;">;</span>
</div>
<div>
<span style="color:#777777;">}</span>)<span style="color:#777777;">;</span>
</div>
<div>
<span style="color:#7a3e9d;">stream</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">on</span>( <span style="color:#777777;">'</span><span style="color:#448c27;">end</span><span style="color:#777777;">'</span><span style="color:#777777;">,</span> <span style="color:#7a3e9d;">function</span><span style="color:#777777;">()</span> <span style="color:#777777;">{</span> <span style="color:#7a3e9d;">var</span> <span style="color:#7a3e9d;">finalData</span> <span style="color:#777777;">=</span> <span style="color:#7a3e9d;font-weight:bold;">Buffer</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">concat</span>( <span style="color:#7a3e9d;">responseData</span> )<span style="color:#777777;">;</span>
</div>
<div>
<span style="color:#7a3e9d;">response</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">write</span>( <span style="color:#7a3e9d;">finalData</span> )<span style="color:#777777;">;</span>
</div>
<div>
<span style="color:#7a3e9d;">response</span><span style="color:#777777;">.</span><span style="color:#aa3731;font-weight:bold;">end</span>()<span style="color:#777777;">;</span>
</div>
<div>
<span style="color:#777777;">}</span>)<span style="color:#777777;">;</span>
</div>
<div>
<span style="color:#777777;">}</span>
</div>
</div>
<li style="box-sizing:border-box;outline:0px;margin:0px;padding:0px 8px;list-style:none;font-size:inherit;overflow-wrap:break-word;color:#999999;">
</li>
-
客户端读取文件 var content = fs.createReadStream(filePath);
-
把内容转为数组 var buffer = new Buffer(content); 传给 httpServer这一步需要特别注意,一定不能把 content 当成普通的字符串处理传给 httpServer
-
httpServer 解析出数组 var list = …
-
httpServer 把数组转为 Buffer, var buffer = new Buffer(list);
-
返回 response.write(buffer.toString(),”binary”); //注意,这里必须转为字符串,不能以 Buffer 传给浏览器.
-
值得注意的是,这里的 buffer 不能直接返回给客户端,nodejs 以 binary 格式读取的文件就是一个字符串,只是编码不是普通的 utf-8
Links: 472