Home  >  Article  >  Web Front-end  >  Detailed explanation of node.js file upload, rename and move location

Detailed explanation of node.js file upload, rename and move location

小云云
小云云Original
2018-05-22 10:30:011879browse

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+&#39;/&#39;+files.file.name,function (a,b) {
  });
  res.json({success:&#39;修改成功&#39;})
 });
};

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn