I am trying to upload a file using my node JS server express. Here is my node code:
var express = expected ('express'); Var app = express (); Var FS = Requirement ('FS'); Var sys = is required ('sys'); App.listen (8080); App.get ('/', function (req, res) {fs.readFile ('upload.html', function (error, data) {res.writeHead (200, {'Content-type': 'text / html' , 'Content-length': data.length}); res.write (data); res.end ();});}); App.post ('/ upload', function (req, res) {console.log (req.files); fs.readFile (req.files.displayImage.path, function (mistake, data) {// ... var Newpath = __dirname; fs.writeFile (newpath, data, function (mistake) {res.redirect ("back");});});}); My upload.html file:
& lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; Upload an example & lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Form id = "upload form" enactepe = "multipart / form-data" action = "/ upload" method = "post" & gt; & Lt; Input type = "file" id = "userphotoInput" name = "displayImage" /> & Lt; Input type = "submit" value = "submit" & gt; & Lt; / Form & gt; & Lt; Span id = "status" / & gt; & Lt; Img id = "uploaded image" /> & Lt; / Body & gt; & Lt; / Html & gt; I'm getting the error that req.files is undefined. What could be wrong? File uploads are also not working.
As outlined in, req.files , Req.body is provided by bodyParser middleware with you can add middleware like this: app.us (express.bodyParser () ); // Or, as `req.files` is provided by Multipart middleware only, you can add // that if you are not related to parsing non-multiprofile uploads, such as: app: use ( Express.multipart ());
No comments:
Post a Comment