Sample Code for renaming and moving node. js files, and node. js sample code
An example of uploading files by node is as follows,
DoUpload () {var formData = new FormData ($ ("# uploadForm") [0]); $. ajax ({url: 'http: // localhost: 3011/upload', type: 'post', data: formData, async: false, cache: false, contentType: false, processData: false, success: function (returndata) {alert (returndata) ;}, error: function (returndata) {// alert (returndata );}});} <form id = "uploadForm"> <p> upload a file: <input id = "UpImage" type = "file" name = "file"/> <input id = "text" type = "text" name = "text" value = "232323 "/> </p> <input type =" button "value =" Upload "onClick = {this. doUpload. bind (this)}/> <input type = "button" onClick = {() => {this. submit () }}value = "OK"/> </form>
The plugin formidable must be installed for the server code.
Exports. when upload = function (req, res, next) {// keepExtensions is true, the file extension var form = new formidable is displayed. incomingForm ({keepExtensions: true}); // specifies the file directory form. uploadDir = path. join (_ dirname); form. parse (req, function (err, fields, files) {// fields stores json data // files stores File Information // Changes the file directory, and the name fs before the upload is displayed. rename (files. file. path ,__ dirname + '/' + files. file. name, function (a, B) {}); res. json ({success: 'Modified successfully '})});};
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.