鍍金池/ 問答/HTML/ node搭建的代理服務(wù)器請(qǐng)求靜態(tài)資源出錯(cuò)

node搭建的代理服務(wù)器請(qǐng)求靜態(tài)資源出錯(cuò)

后臺(tái)返回給我的接口中,有一個(gè)圖片的絕對(duì)路徑。比如后臺(tái)ip是11.11.1,圖片路徑是/res/default.jpg。我在網(wǎng)址中輸入11.11.1/res/default.jpg可以看到圖片,但是卻顯示不出來
我是通過客戶端發(fā)送請(qǐng)求,用node寫了代理,得到響應(yīng)后,拿到圖片路徑信息后給到img標(biāo)簽的src屬性。

這是我的node代理代碼

else if (pathname == '/res/default.jpg') {
        (function() {                             
            var options = {
                host: "11.11.1", 
                path: pathname, 
                method: 'get' 
            };
            console.log(pathname);
            let req = http.request(options, function(req) { 
                req.on("data", function(chunk) {
                    sendmsg += chunk; 
                });
                req.on("end", function(d) { 
                    var list = JSON.stringify(sendmsg); 
                    response.writeHead(200);
                    response.end(sendmsg);
                });
            });
            req.end(); 
        })()
    }      

然后的js發(fā)送請(qǐng)求代碼

        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
                    var text = JSON.parse(xhr.responseText);
                    var imgsrc= text["data"]["list"][0]["picture"];
                    document.getElementsByTagName('img')[0].setAttribute('src', imgsrc);
                } else {
                    alert("error: " + xhr.status);
                }
            }
        }
        var url = "/Blog/listAll/1"
        xhr.open("get", url, true);
        xhr.send(null);

在瀏覽器中圖片的網(wǎng)址是:http://127.0.0.1:8080/res/default.jpg。 但是我發(fā)現(xiàn)它的type是octet-stream,不知道會(huì)不會(huì)是這里的問題

圖片描述

回答
編輯回答
神曲

是,這是默認(rèn)值,意思是未知的文件。

補(bǔ)充

Web

瀏覽器確定格式只根據(jù)MIME,如果你一個(gè)css文件返回的Content-Typeimage/jpeg實(shí)際上他也會(huì)當(dāng)這個(gè)文件是圖片,就算你是.css后綴。

附件

瀏覽器確定格式僅根據(jù)文件后綴以及文件的具體格式(比如.jpg.txt)解析。

回到問題,application/octet-stream表示未知文件,表明某種二進(jìn)制流數(shù)據(jù),此時(shí)瀏覽器的行為是下載。而通過地址欄打開的意思是,下載成本地附件,然后通過瀏覽器打開附件。

2017年2月19日 03:48