This article mainly introduces the sample code for uploading, renaming and moving the node.js file. Now I share it with you and give it as a reference.
An example about node uploading files, the following is the front-end code,
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>上传文件: <input id="UpImage" type="file" name="file"/> <input id="text" type="text" name="text" value="232323"/> </p> <input type="button" value="上传" onClick={this.doUpload.bind(this)}/> <input type="button" onClick={()=>{this.submit()}} value="确定"/> </form>
The server code needs to install the plug-in formidable
exports.upload = function (req,res,next) { //keepExtensions为true时,显示文件扩展名 var form = new formidable.IncomingForm({keepExtensions:true}); //指定文件目录 form.uploadDir = path.join(__dirname); form.parse(req,function (err,fields,files) { //fields存放的为json数据 //files存放的是文件信息 //更改文件目录,并且显示上传之前的名字 fs.rename(files.file.path,__dirname+'/'+files.file.name,function (a,b) { }); res.json({success:'修改成功'}) }); };
The above is what I compiled for everyone, I hope that in the future It will be helpful to everyone.
Related articles:
How to achieve seamless scrolling effect using vue.js
Use routing in vue to achieve page refresh
jQuery implements the recursive infinite layer function
How to implement the chain of responsibility pattern in JavaScript
Use Ajax and Jquery implements the secondary linkage of the drop-down box
How does Jquery dynamically obtain data in the drop-down box
Questions about the vue-awesome-swiper plug-in
The above is the detailed content of How to implement file upload renaming in node.js?. For more information, please follow other related articles on the PHP Chinese website!