使用Node.js重命名文件

时间:2018-05-07 00:48:28

标签: javascript node.js

我是节点新手并试图找到一种方法从目录中加载一些对象中的pdf文件。使用输入,然后根据输入的用户输入重命名文件。我有2个问题。

1问题是fs.readdirSync(目录);程序启动时调用正在运行。我需要在页面加载时进行调用。 第二个问题是我没有找到如何获取输入的值并使用fs.rename重命名文件。或者在必要时创建新文件。 (这将是一个仅在小型办公室本地使用的内部应用程序,用于为特定目的重命名pdf文件。不是Web应用程序。只需要一个简单的用户界面UI。) index.js

var express = require('express');
var app = express();
var path = require('path');
var formidable = require('formidable');
var ejs = require('ejs');
var fs = require('fs');

// set the view engine to ejs
app.set('view engine', 'ejs');

app.use(express.static(path.join(__dirname, 'public')));

app.use('/img', express.static(__dirname + '/img'));
//fs





let directory = "img";
let dirBuf = Buffer.from(directory);

// Buffer mydata


  function bufferFile(relPath) {
    return fs.readdirSync(path.join(__dirname, relPath));
    console.log("hello from bufferFile"); // zzzz....
  }

// let files = fs.readdirSync(directory);
// console.log(files, "This is the fs.readdirSync");
let files = fs.readdirSync(directory);
console.log(files, "This is the fs.readdirSync");

//routes
app.get('/', function(req, res){
  res.sendFile(path.join(__dirname, 'views/index.html'));
});

//routes
app.get('/new', function(req, res){

// fs.readdir(specs, (err, files) => {
//   files.forEach(file => {

//   });
// })

  res.render('rename',{


        files: files,

     });

});
app.get('/rename', function(req,res){
  res.render('rename',{


       files: files,
       dirBuf: dirBuf
     });

});


app.post('/upload', function(req, res){

  // create an incoming form object
  var form = new formidable.IncomingForm();

  // specify that we want to allow the user to upload multiple files in a single request
  form.multiples = true;

  // store all uploads in the /uploads directory
  form.uploadDir = path.join(__dirname, '/img');

  // every time a file has been uploaded successfully,
  // rename it to it's orignal name
  form.on('file', function(field, file) {
    fs.rename(file.path, path.join(form.uploadDir, file.name));
  });

  // log any errors that occur
  form.on('error', function(err) {
    console.log('An error has occured: \n' + err);
  });

  // once all the files have been uploaded, send a response to the client
  form.on('end', function() {
    res.end('success');

  });

  // parse the incoming request containing the form data
  form.parse(req);

});

var server = app.listen(3000, function(){
  console.log('Server listening on port 3000');
});

rename.ejs

<!-- views/partials/head.ejs -->
 <% include ./partials/head %>





<ul>

    <% files.forEach(function(files) { %>

        <div class="col-lg-6">
        <div class="form-group">
        <h3><%= files %></h3>
        <object data='img/<%= files %>' type='application/pdf' width='100%' height='250px'></object>
        <input class="form-control" id="<%=files.name%>" value="<%=files.name%>">

        </div>
        </div>
    <% }); %>
</ul>

<button type="submit" class="btn btn-primary">Submit</button>


 <% include ./partials/footer%>
<!-- views/partials/footer.ejs -->

0 个答案:

没有答案