Home >Web Front-end >JS Tutorial >Detailed explanation of node.js file upload, rename and move location
This article mainly introduces the sample code for uploading, renaming and moving the node.js file. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
An example of uploading files on node. 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-side 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:'修改成功'}) }); };
Related recommendations :
node.js file upload processing example
Detailed explanation of Node layer simulation to implement multipart form file upload
Detailed explanation of using nodejs+express to implement simple file upload function
The above is the detailed content of Detailed explanation of node.js file upload, rename and move location. For more information, please follow other related articles on the PHP Chinese website!