中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

NodeJS系列(lie)~第(di)二個(ge)小例子,解(jie)決中文亂碼的問題

返回目錄

為了使頁(ye)面(mian)支持的字符(fu)更多,所以(yi)編(bian)(bian)(bian)碼使用UTF-8,注意(yi),你的原(yuan)js文件的編(bian)(bian)(bian)碼也要是utf-8的,可以(yi)通過記事本進行編(bian)(bian)(bian)碼的修改

然后在你(ni)的js服(fu)務(wu)端添加網頁響應頭(tou)信息,把charset:utf8添加到頭(tou)中,代碼(ma)如下(xia)

function start(response, request) {
    var getQuery = url.parse(request.url).query;
    var getData = qs.parse(getQuery); //getData數據 
    var body =
       '<form action="/upload" enctype="multipart/form-data" ' +
       'method="post">' +
       '選擇(ze)文件:<input type="file" name="upload" multiple="multiple">' +
       '<input type="submit" value="Upload file">' +
       '</form>';
    response.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' });
    // response.write(getData["jsonpcallback"] + "({result:'" + body + "'})");//輸出(chu)json
    response.write(body);//輸(shu)出字(zi)符串
    response.end();
}

當然訪問這個頁面時(shi),中(zhong)文就(jiu)可以順序的(de)顯示出(chu)來(lai)了,呵呵

小知識,一(yi)般(ban)為了隱藏(zang)圖(tu)像(xiang)(xiang)文件(jian)的地址(zhi),或者統一(yi)為圖(tu)像(xiang)(xiang)添加某些信息(xi)(如文字,水紋等),我(wo)們會(hui)通過(guo)一(yi)個網(wang)頁來響應圖(tu)像(xiang)(xiang)文件(jian),你可(ke)以將圖(tu)像(xiang)(xiang)ID傳入網(wang)頁,然后網(wang)頁以  "Content-Type": "image/jpg"的格(ge)式進行響應即(ji)可(ke)

//顯示圖像
function show(response, request) {
    console.log('request handler \'show\' was called...')
    console.log("read image:" + filename);
    fs.readFile(filename, "binary", function (error, file) {
        if (error) {
            response.writeHead(500, {
                "Content-Type": "text/plain"
            });
            response.write(error + "\n");
            response.end();
        } else {
            response.writeHead(200, {
                "Content-Type": "image/jpg"
            });
            response.write(file, "binary");
            response.end();
        }
    });
}

結果如圖:

 

返回目錄

posted @ 2013-11-21 10:03  張占嶺  閱讀(18304)  評論(1)    收藏  舉報