nodejs-expressjs上传图像并显示它们
nodejs expressjs upload images and display them
我有以下代码
app.js
var express = require('express'),
fs = require('fs'),
format = require('util').format;
var app = express();
app.configure(function() {
app.use(express.bodyParser());
app.use(express.bodyParser({uploadDir:'./static/files/'}))
app.use(express.methodOverride());
app.use(express.static(__dirname + '/static'));
app.use(express.errorHandler({
dumpException: true,
showStack: true
}));
});
app.set('views',__dirname + '/views');
app.set('view engine', 'jade');
好的,那么我有这条路线
app.post('/addPhoto', products.addPhoto);
此表单
form(action='/new', method='POST', enctype="multipart/form-data")
input(type='text', name="name", placeholder='nanme')
input(type='text', name="description",placeholder='description')
input(type='text', name="thumbnail_img",placeholder='path')
input(type='number', name='price', placeholder='123')
input(type="file", name="thumbnail")
input(type='submit')
我不顾一切地使用thumbnail_img来修改的路径
现在在products.js 中
var products = exports.products = [];
var format = require('util').format;
var fs = require('fs');
products.push(
{ id:0,
name:'name 1',
description:'description1',
thumbnail_img: '',
price: 100 },
{ id:1,
name: 'name_2',
description: 'description2',
thumbnail_img: '',
price: 150
}
);
exports.addPhoto = function(req,res){
var body = req.body;
var tmp_path = req.files.thumbnail.path;
var target_path = './static/files/' + req.files.thumbnail.name;
fs.rename(tmp_path, target_path, function(err) {
if (err) throw err;
fs.unlink(tmp_path, function() {
body.thumbnail_img = target_path;
if (err) throw err;
});
});
var body = req.body;
// Fun here is I got a path starting with a .
// so this code is for remove the .
var n = target_path.toString()
var n2 = n.replace('.','');
body.thumbnail_img = n2;
products.push(req.body);
res.redirect('/products');
};
问题是,当我使用模板来显示产品时,src属性显示如下src="./static/files/name-fo-the-pics"
,所以我如何显示我上传的图像
在这种情况下,您可以得到存储文件的目标路径,并将其保存到数据库中。
使用此方法上传
app.post('/upload', function(req, res) {
// get the temporary location of the file
var tmp_path = req.files.thumbnail.path;
// set where the file should actually exists - in this case it is in the "images" directory
target_path = '/tmp/' + req.files.thumbnail.name;
// move the file from the temporary location to the intended location
fs.rename(tmp_path, target_path, function(err) {
if (err) throw err;
// delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
fs.unlink(tmp_path, function() {
if (err) throw err;
});
});
});
当检索显示该方法中的路径
fs.readFile(target_path, "binary", function(error, file) {
if(error) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.write(error + "'n");
res.end();
} else {
res.writeHead(200, {"Content-Type": "image/png"});
res.write(file, "binary");
}
那应该行得通。
相关文章:
- 使用JSP从服务器检索和显示图像
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 使用FormData上传AJAX图像;t在服务器端显示图像
- 选择文件后显示图像
- Chrome(webkit?)无法在幻灯片中正确显示图像
- 如何使用javascript显示图像
- 使用JS按顺序显示图像,而不是随机显示
- 使用服务器上的HTML/js在网页上显示图像
- 我想在选择输入文件上显示图像
- Javascript:单击单选按钮时显示图像
- 如何将JSON结果设置为'src'共'img'以在ASP.NET MVC4中显示图像
- 无法在cakepp布局中显示图像
- 页面加载后通过javascript显示图像
- 将图像文件存储在猫鼬模式中的二进制数据中,并以html形式显示图像
- 使用AngularJs数据绑定的三元运算符显示图像
- 加载时画布上未显示图像
- 运行POST时显示图像
- NodeJS上没有显示图像
- 从Javascript在表中显示图像
- 如何在MVC5中运行时在HTML5 Canvas中显示图像