How stop redirect on form submission, but still POST to action URL with Multer?












-1















I'm using Multer to upload images to the server filesystem, and the tutorial I followed suggested to have the form HTML setup as follows:



<form action="/upload" id="myForm" name="myForm" enctype="multipart/form-data" method="post">
<input id="myFile" name="myFile" type="file">
<button type="submit" class="btn btn-primary">Submit</button>
</form>


The image file uploads successful, however the user will be redirected to "/upload". From what I researched, Ajax can't handle image uploads and Multer needs the enctype="multipart/form-data" attribute anyways.



I had attempted to setup an express route to redirect the user to the previous page, but it seems like express can't get the route since neither of the console.log() are being executed. Here is the code I had for that:



 app.get("/upload", function(req, res){
backURL = req.header('Referer') || '/';
// need to display "Profile image updated!"
console.log(backURL);
console.log("none");
res.redirect(backURL);
})


Is there some way to stop the redirect yet still POST to /upload?



EDIT: I forgot to mention that I am also using express to post to post the data to /upload:



  app.post('/upload', function (req, res) {
upload(req, res, (err) => {
if (err) {
console.log("problem at api-routes line 88")
res.render('index', {
msg: err
});
} else {
if(req.file == undefined){
res.render('index', {
msg: 'Error: No File Selected!'
});
} else {
db.User.update({
profileImg: req.file.filename
},{
where: {
id: req.user.id
}
}).then(function (dbPost) {
//res.json(dbPost);
res.render('index', {
msg: 'File Uploaded!',
file: `uploads/${req.file.filename}`
});
}).catch(function (err) {
console.log(err);
res.status(422).json(err.errors[0].message);
});

}
}
})


})










share|improve this question

























  • "Ajax can't handle image uploads" - incorrect: developer.mozilla.org/en-US/docs/Web/API/FormData/…

    – Quentin
    Nov 23 '18 at 8:13
















-1















I'm using Multer to upload images to the server filesystem, and the tutorial I followed suggested to have the form HTML setup as follows:



<form action="/upload" id="myForm" name="myForm" enctype="multipart/form-data" method="post">
<input id="myFile" name="myFile" type="file">
<button type="submit" class="btn btn-primary">Submit</button>
</form>


The image file uploads successful, however the user will be redirected to "/upload". From what I researched, Ajax can't handle image uploads and Multer needs the enctype="multipart/form-data" attribute anyways.



I had attempted to setup an express route to redirect the user to the previous page, but it seems like express can't get the route since neither of the console.log() are being executed. Here is the code I had for that:



 app.get("/upload", function(req, res){
backURL = req.header('Referer') || '/';
// need to display "Profile image updated!"
console.log(backURL);
console.log("none");
res.redirect(backURL);
})


Is there some way to stop the redirect yet still POST to /upload?



EDIT: I forgot to mention that I am also using express to post to post the data to /upload:



  app.post('/upload', function (req, res) {
upload(req, res, (err) => {
if (err) {
console.log("problem at api-routes line 88")
res.render('index', {
msg: err
});
} else {
if(req.file == undefined){
res.render('index', {
msg: 'Error: No File Selected!'
});
} else {
db.User.update({
profileImg: req.file.filename
},{
where: {
id: req.user.id
}
}).then(function (dbPost) {
//res.json(dbPost);
res.render('index', {
msg: 'File Uploaded!',
file: `uploads/${req.file.filename}`
});
}).catch(function (err) {
console.log(err);
res.status(422).json(err.errors[0].message);
});

}
}
})


})










share|improve this question

























  • "Ajax can't handle image uploads" - incorrect: developer.mozilla.org/en-US/docs/Web/API/FormData/…

    – Quentin
    Nov 23 '18 at 8:13














-1












-1








-1








I'm using Multer to upload images to the server filesystem, and the tutorial I followed suggested to have the form HTML setup as follows:



<form action="/upload" id="myForm" name="myForm" enctype="multipart/form-data" method="post">
<input id="myFile" name="myFile" type="file">
<button type="submit" class="btn btn-primary">Submit</button>
</form>


The image file uploads successful, however the user will be redirected to "/upload". From what I researched, Ajax can't handle image uploads and Multer needs the enctype="multipart/form-data" attribute anyways.



I had attempted to setup an express route to redirect the user to the previous page, but it seems like express can't get the route since neither of the console.log() are being executed. Here is the code I had for that:



 app.get("/upload", function(req, res){
backURL = req.header('Referer') || '/';
// need to display "Profile image updated!"
console.log(backURL);
console.log("none");
res.redirect(backURL);
})


Is there some way to stop the redirect yet still POST to /upload?



EDIT: I forgot to mention that I am also using express to post to post the data to /upload:



  app.post('/upload', function (req, res) {
upload(req, res, (err) => {
if (err) {
console.log("problem at api-routes line 88")
res.render('index', {
msg: err
});
} else {
if(req.file == undefined){
res.render('index', {
msg: 'Error: No File Selected!'
});
} else {
db.User.update({
profileImg: req.file.filename
},{
where: {
id: req.user.id
}
}).then(function (dbPost) {
//res.json(dbPost);
res.render('index', {
msg: 'File Uploaded!',
file: `uploads/${req.file.filename}`
});
}).catch(function (err) {
console.log(err);
res.status(422).json(err.errors[0].message);
});

}
}
})


})










share|improve this question
















I'm using Multer to upload images to the server filesystem, and the tutorial I followed suggested to have the form HTML setup as follows:



<form action="/upload" id="myForm" name="myForm" enctype="multipart/form-data" method="post">
<input id="myFile" name="myFile" type="file">
<button type="submit" class="btn btn-primary">Submit</button>
</form>


The image file uploads successful, however the user will be redirected to "/upload". From what I researched, Ajax can't handle image uploads and Multer needs the enctype="multipart/form-data" attribute anyways.



I had attempted to setup an express route to redirect the user to the previous page, but it seems like express can't get the route since neither of the console.log() are being executed. Here is the code I had for that:



 app.get("/upload", function(req, res){
backURL = req.header('Referer') || '/';
// need to display "Profile image updated!"
console.log(backURL);
console.log("none");
res.redirect(backURL);
})


Is there some way to stop the redirect yet still POST to /upload?



EDIT: I forgot to mention that I am also using express to post to post the data to /upload:



  app.post('/upload', function (req, res) {
upload(req, res, (err) => {
if (err) {
console.log("problem at api-routes line 88")
res.render('index', {
msg: err
});
} else {
if(req.file == undefined){
res.render('index', {
msg: 'Error: No File Selected!'
});
} else {
db.User.update({
profileImg: req.file.filename
},{
where: {
id: req.user.id
}
}).then(function (dbPost) {
//res.json(dbPost);
res.render('index', {
msg: 'File Uploaded!',
file: `uploads/${req.file.filename}`
});
}).catch(function (err) {
console.log(err);
res.status(422).json(err.errors[0].message);
});

}
}
})


})







html node.js express multer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 8:14







Revircs

















asked Nov 23 '18 at 8:08









RevircsRevircs

8211




8211













  • "Ajax can't handle image uploads" - incorrect: developer.mozilla.org/en-US/docs/Web/API/FormData/…

    – Quentin
    Nov 23 '18 at 8:13



















  • "Ajax can't handle image uploads" - incorrect: developer.mozilla.org/en-US/docs/Web/API/FormData/…

    – Quentin
    Nov 23 '18 at 8:13

















"Ajax can't handle image uploads" - incorrect: developer.mozilla.org/en-US/docs/Web/API/FormData/…

– Quentin
Nov 23 '18 at 8:13





"Ajax can't handle image uploads" - incorrect: developer.mozilla.org/en-US/docs/Web/API/FormData/…

– Quentin
Nov 23 '18 at 8:13












1 Answer
1






active

oldest

votes


















-1















The image file uploads successful, however the user will be redirected to "/upload".




A redirect happens when the browser asks for one URL but the server says "What you want it at this other URL, get that instead".



You aren't experiencing a redirect. The action just tells the browser to submit the data to /upload.






I had attempted to setup an express route to redirect the user to the previous page, but it seems like express can't get the route since neither of the console.log() are being executed.




Your route is for get() but you have method="POST".



To handle a request for a POST request you need a post() handler. The same one that actually processes the form submission!






share|improve this answer
























  • Seems I edited the post as you were submitting this answer. I have a post route that is using Multer's upload()

    – Revircs
    Nov 23 '18 at 8:23











  • @Revircs — The answer stands. You need to put the code you want to run when the POST request is made in the post handler. You've put it in a get handler.

    – Quentin
    Nov 24 '18 at 0:03











  • Oh wow, I apologize. I understand what you are saying now. Thank you very much for the help!

    – Revircs
    Nov 24 '18 at 19:51











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442781%2fhow-stop-redirect-on-form-submission-but-still-post-to-action-url-with-multer%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









-1















The image file uploads successful, however the user will be redirected to "/upload".




A redirect happens when the browser asks for one URL but the server says "What you want it at this other URL, get that instead".



You aren't experiencing a redirect. The action just tells the browser to submit the data to /upload.






I had attempted to setup an express route to redirect the user to the previous page, but it seems like express can't get the route since neither of the console.log() are being executed.




Your route is for get() but you have method="POST".



To handle a request for a POST request you need a post() handler. The same one that actually processes the form submission!






share|improve this answer
























  • Seems I edited the post as you were submitting this answer. I have a post route that is using Multer's upload()

    – Revircs
    Nov 23 '18 at 8:23











  • @Revircs — The answer stands. You need to put the code you want to run when the POST request is made in the post handler. You've put it in a get handler.

    – Quentin
    Nov 24 '18 at 0:03











  • Oh wow, I apologize. I understand what you are saying now. Thank you very much for the help!

    – Revircs
    Nov 24 '18 at 19:51
















-1















The image file uploads successful, however the user will be redirected to "/upload".




A redirect happens when the browser asks for one URL but the server says "What you want it at this other URL, get that instead".



You aren't experiencing a redirect. The action just tells the browser to submit the data to /upload.






I had attempted to setup an express route to redirect the user to the previous page, but it seems like express can't get the route since neither of the console.log() are being executed.




Your route is for get() but you have method="POST".



To handle a request for a POST request you need a post() handler. The same one that actually processes the form submission!






share|improve this answer
























  • Seems I edited the post as you were submitting this answer. I have a post route that is using Multer's upload()

    – Revircs
    Nov 23 '18 at 8:23











  • @Revircs — The answer stands. You need to put the code you want to run when the POST request is made in the post handler. You've put it in a get handler.

    – Quentin
    Nov 24 '18 at 0:03











  • Oh wow, I apologize. I understand what you are saying now. Thank you very much for the help!

    – Revircs
    Nov 24 '18 at 19:51














-1












-1








-1








The image file uploads successful, however the user will be redirected to "/upload".




A redirect happens when the browser asks for one URL but the server says "What you want it at this other URL, get that instead".



You aren't experiencing a redirect. The action just tells the browser to submit the data to /upload.






I had attempted to setup an express route to redirect the user to the previous page, but it seems like express can't get the route since neither of the console.log() are being executed.




Your route is for get() but you have method="POST".



To handle a request for a POST request you need a post() handler. The same one that actually processes the form submission!






share|improve this answer














The image file uploads successful, however the user will be redirected to "/upload".




A redirect happens when the browser asks for one URL but the server says "What you want it at this other URL, get that instead".



You aren't experiencing a redirect. The action just tells the browser to submit the data to /upload.






I had attempted to setup an express route to redirect the user to the previous page, but it seems like express can't get the route since neither of the console.log() are being executed.




Your route is for get() but you have method="POST".



To handle a request for a POST request you need a post() handler. The same one that actually processes the form submission!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 8:12









QuentinQuentin

656k728911055




656k728911055













  • Seems I edited the post as you were submitting this answer. I have a post route that is using Multer's upload()

    – Revircs
    Nov 23 '18 at 8:23











  • @Revircs — The answer stands. You need to put the code you want to run when the POST request is made in the post handler. You've put it in a get handler.

    – Quentin
    Nov 24 '18 at 0:03











  • Oh wow, I apologize. I understand what you are saying now. Thank you very much for the help!

    – Revircs
    Nov 24 '18 at 19:51



















  • Seems I edited the post as you were submitting this answer. I have a post route that is using Multer's upload()

    – Revircs
    Nov 23 '18 at 8:23











  • @Revircs — The answer stands. You need to put the code you want to run when the POST request is made in the post handler. You've put it in a get handler.

    – Quentin
    Nov 24 '18 at 0:03











  • Oh wow, I apologize. I understand what you are saying now. Thank you very much for the help!

    – Revircs
    Nov 24 '18 at 19:51

















Seems I edited the post as you were submitting this answer. I have a post route that is using Multer's upload()

– Revircs
Nov 23 '18 at 8:23





Seems I edited the post as you were submitting this answer. I have a post route that is using Multer's upload()

– Revircs
Nov 23 '18 at 8:23













@Revircs — The answer stands. You need to put the code you want to run when the POST request is made in the post handler. You've put it in a get handler.

– Quentin
Nov 24 '18 at 0:03





@Revircs — The answer stands. You need to put the code you want to run when the POST request is made in the post handler. You've put it in a get handler.

– Quentin
Nov 24 '18 at 0:03













Oh wow, I apologize. I understand what you are saying now. Thank you very much for the help!

– Revircs
Nov 24 '18 at 19:51





Oh wow, I apologize. I understand what you are saying now. Thank you very much for the help!

– Revircs
Nov 24 '18 at 19:51




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442781%2fhow-stop-redirect-on-form-submission-but-still-post-to-action-url-with-multer%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]