为了使页面支持的字符更多,所以编码使用UTF-8,注意,你的原js文件的编码也要是utf-8的,可以通过记事本进行编码的修改
然后在你的js服务端添加网页响应头信息,把charset:utf8添加到头中,代码如下
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">' +
'选择文件:<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 + "'})");//输出json
response.write(body);//输出字符串
response.end();
}
当然访问这个页面时,中文就可以顺序的显示出来了,呵呵
小知识,一般为了隐藏图像文件的地址,或者统一为图像添加某些信息(如文字,水纹等),我们会通过一个网页来响应图像文件,你可以将图像ID传入网页,然后网页以 "Content-Type": "image/jpg"的格式进行响应即可
//显示图像
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();
}
});
}
结果如图:
作者:仓储大叔,张占岭,
荣誉:微软MVP