Python and Node : Image Upload
I'm pretty new to Python and Node, I'm attempting to do a simple image upload. Python for the client side and Node for the server side. The image is uploaded and looks to be the correct size, but when I try to open or preview the image I get a prompt saying the following :
"The file could not be opened . It may be damaged or use a file format that Preview doesn’t recognise"
NOTE* I was able to get the upload to work using multer, see my posted answer. However , I would still I like to figure out why image open or image preview fails.
Client code (test locally) :
import requests
url = "http://localhost:3000/upload"
image = open('test1.jpg','rb').read()
file = {'file': image}
r = requests.post(url, files=file)
print(r.status_code)
print(r.text)
Server code snippet :
const Fs = require('fs')
...
...
router.post('/upload', (req, res) =>{
var data = '';
req.setEncoding('binary');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function(){
Fs.writeFile("tester.jpg", data, 'binary', function(err) {
if(err) throw err
res.send("Image uploaded ...");
});
})
})
node.js python-3.x
add a comment |
I'm pretty new to Python and Node, I'm attempting to do a simple image upload. Python for the client side and Node for the server side. The image is uploaded and looks to be the correct size, but when I try to open or preview the image I get a prompt saying the following :
"The file could not be opened . It may be damaged or use a file format that Preview doesn’t recognise"
NOTE* I was able to get the upload to work using multer, see my posted answer. However , I would still I like to figure out why image open or image preview fails.
Client code (test locally) :
import requests
url = "http://localhost:3000/upload"
image = open('test1.jpg','rb').read()
file = {'file': image}
r = requests.post(url, files=file)
print(r.status_code)
print(r.text)
Server code snippet :
const Fs = require('fs')
...
...
router.post('/upload', (req, res) =>{
var data = '';
req.setEncoding('binary');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function(){
Fs.writeFile("tester.jpg", data, 'binary', function(err) {
if(err) throw err
res.send("Image uploaded ...");
});
})
})
node.js python-3.x
add a comment |
I'm pretty new to Python and Node, I'm attempting to do a simple image upload. Python for the client side and Node for the server side. The image is uploaded and looks to be the correct size, but when I try to open or preview the image I get a prompt saying the following :
"The file could not be opened . It may be damaged or use a file format that Preview doesn’t recognise"
NOTE* I was able to get the upload to work using multer, see my posted answer. However , I would still I like to figure out why image open or image preview fails.
Client code (test locally) :
import requests
url = "http://localhost:3000/upload"
image = open('test1.jpg','rb').read()
file = {'file': image}
r = requests.post(url, files=file)
print(r.status_code)
print(r.text)
Server code snippet :
const Fs = require('fs')
...
...
router.post('/upload', (req, res) =>{
var data = '';
req.setEncoding('binary');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function(){
Fs.writeFile("tester.jpg", data, 'binary', function(err) {
if(err) throw err
res.send("Image uploaded ...");
});
})
})
node.js python-3.x
I'm pretty new to Python and Node, I'm attempting to do a simple image upload. Python for the client side and Node for the server side. The image is uploaded and looks to be the correct size, but when I try to open or preview the image I get a prompt saying the following :
"The file could not be opened . It may be damaged or use a file format that Preview doesn’t recognise"
NOTE* I was able to get the upload to work using multer, see my posted answer. However , I would still I like to figure out why image open or image preview fails.
Client code (test locally) :
import requests
url = "http://localhost:3000/upload"
image = open('test1.jpg','rb').read()
file = {'file': image}
r = requests.post(url, files=file)
print(r.status_code)
print(r.text)
Server code snippet :
const Fs = require('fs')
...
...
router.post('/upload', (req, res) =>{
var data = '';
req.setEncoding('binary');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function(){
Fs.writeFile("tester.jpg", data, 'binary', function(err) {
if(err) throw err
res.send("Image uploaded ...");
});
})
})
node.js python-3.x
node.js python-3.x
edited Nov 14 '18 at 16:05
Fabii
asked Nov 14 '18 at 3:29
FabiiFabii
1,80183778
1,80183778
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I was able to sort it out using multer : npm multer
var Storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./images");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
}
});
var upload = multer({
storage: Storage
}).single('file');
router.post('/upload', (req, res) =>{
console.log("upload ...");
upload(req, res, (err) =>{
if(err){
console.log(err);
res.send("Failed")
}
else{
console.log(req.file);
res.send("Uploaded ...");
}
});
})
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292778%2fpython-and-node-image-upload%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I was able to sort it out using multer : npm multer
var Storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./images");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
}
});
var upload = multer({
storage: Storage
}).single('file');
router.post('/upload', (req, res) =>{
console.log("upload ...");
upload(req, res, (err) =>{
if(err){
console.log(err);
res.send("Failed")
}
else{
console.log(req.file);
res.send("Uploaded ...");
}
});
})
add a comment |
I was able to sort it out using multer : npm multer
var Storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./images");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
}
});
var upload = multer({
storage: Storage
}).single('file');
router.post('/upload', (req, res) =>{
console.log("upload ...");
upload(req, res, (err) =>{
if(err){
console.log(err);
res.send("Failed")
}
else{
console.log(req.file);
res.send("Uploaded ...");
}
});
})
add a comment |
I was able to sort it out using multer : npm multer
var Storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./images");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
}
});
var upload = multer({
storage: Storage
}).single('file');
router.post('/upload', (req, res) =>{
console.log("upload ...");
upload(req, res, (err) =>{
if(err){
console.log(err);
res.send("Failed")
}
else{
console.log(req.file);
res.send("Uploaded ...");
}
});
})
I was able to sort it out using multer : npm multer
var Storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./images");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
}
});
var upload = multer({
storage: Storage
}).single('file');
router.post('/upload', (req, res) =>{
console.log("upload ...");
upload(req, res, (err) =>{
if(err){
console.log(err);
res.send("Failed")
}
else{
console.log(req.file);
res.send("Uploaded ...");
}
});
})
answered Nov 14 '18 at 4:21
FabiiFabii
1,80183778
1,80183778
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292778%2fpython-and-node-image-upload%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown