admin管理员组文章数量:1310067
I have a simple server which has this method
app.post('/', function (req, res) {
res.sendfile(path.resolve(req.files.image.path));
});
How do I get data, on client side in Image object? this is my ajax.success method,at least what i tried...
success: function (res) {
console.log(res);
var canvas = document.getElementById("mainCanvas");
var ctx = canvas.getContext("2d");
var img = new Image();
img.onload = function () {
ctx.drawImage(img,0,0);
}
img.src=res
}
Really looking for answer already for 2 days... tried a lot of ways, but none worked. I am not even sure what I receive from server - is it bytes array?
SOLUTION: so, i figured out that post request does not need to send file back, Image.src sends its own get request to server
app.post('/', function (req, res) {
res.send(path.basename(req.files.image.path));
});
/* serves all the static files */
app.get(/^(.+)$/, function(req, res){
console.log('static file request : ' + req.params);
res.sendfile( __dirname + req.params[0]);
});
client:
success: function (res) {
var canvas = document.getElementById("mainCanvas");
var ctx = canvas.getContext("2d");
var img = new Image();
console.log(res);
img.onload = function () {
ctx.drawImage(img,0,0);
}
img.src="/uploads/"+res;
}
I have a simple server which has this method
app.post('/', function (req, res) {
res.sendfile(path.resolve(req.files.image.path));
});
How do I get data, on client side in Image object? this is my ajax.success method,at least what i tried...
success: function (res) {
console.log(res);
var canvas = document.getElementById("mainCanvas");
var ctx = canvas.getContext("2d");
var img = new Image();
img.onload = function () {
ctx.drawImage(img,0,0);
}
img.src=res
}
Really looking for answer already for 2 days... tried a lot of ways, but none worked. I am not even sure what I receive from server - is it bytes array?
SOLUTION: so, i figured out that post request does not need to send file back, Image.src sends its own get request to server
app.post('/', function (req, res) {
res.send(path.basename(req.files.image.path));
});
/* serves all the static files */
app.get(/^(.+)$/, function(req, res){
console.log('static file request : ' + req.params);
res.sendfile( __dirname + req.params[0]);
});
client:
success: function (res) {
var canvas = document.getElementById("mainCanvas");
var ctx = canvas.getContext("2d");
var img = new Image();
console.log(res);
img.onload = function () {
ctx.drawImage(img,0,0);
}
img.src="/uploads/"+res;
}
Share
Improve this question
edited Dec 6, 2013 at 10:55
Alexander Capone
asked Dec 6, 2013 at 0:28
Alexander CaponeAlexander Capone
5583 silver badges17 bronze badges
1
- What does your console.log report? And what other parameters are you passing to $.ajax? Looking at the canvas docs (html5canvastutorials./tutorials/html5-canvas-images) suggests img.src should be the string filename (i.e. location on your server) of the image. At which point, I imagine it just works (kind of the way an image tag in html does). – tandrewnichols Commented Dec 6, 2013 at 2:12
1 Answer
Reset to default 7You are trying to set the src
attribute of the image to the byte code of the image you returned, which will not work. You need to set it to the path of the image that you want to display. The Image object will perform a GET request to your server on its own, so there is no need for an ajax request. Something like the following should work for you:
client:
var canvas = document.getElementById("mainCanvas");
var ctx = canvas.getContext("2d");
var img = new Image();
img.src = "/imagepath.png";
img.onload = function () {
ctx.drawImage(img,0,0);
}
server:
app.get('/imagepath.png', function (req, res) {
res.sendfile(path.resolve(path.resolve(__dirname,'/imagepath.png')));
});
本文标签:
版权声明:本文标题:node.js - How to open image in javascript,sent by express.js sendfile function[+SOLUTION] - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741856082a2401349.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论