如何在Node中找出文件大小?

时间:2016-09-06 18:26:14

标签: javascript jquery node.js

我有一个应用程序来推断用户上传的文件的大小。我的代码如下

server.js代码如下

var express = require('express');
var formidable = require('express-formidable');
var fs = require('fs');

var app = express();

var PORT = 8080;

app.use(express.static(__dirname+'/views'));

app.use(formidable.parse());

app.set('views', './views');

app.set('view engine', 'jade');

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

当我尝试记录下面的变量统计信息时,我收到错误消息

  

TypeError:path必须是字符串

app.post('/upload', function(req, res){
   var stats = fs.statSync(req.body);
   console.log(stats);
});

app.listen(PORT, function(){
    console.log('Express listening on port: '+PORT);
});

index.jade文件呈现在

下面
html
    head
        link(rel='stylesheet', href='style.css', type='text/css')
        title Upload file for shortening
    body
        h1 Welcome to file metadata service
        div(id='upload-button')
            form(action='/upload', enctype='multipart/form-data')
                input(name='Upload', type='file', id='upload-butt')
        div(id="submit-button")
            form(action = '/submit')
                button(type="submit", value='Submit', id='submit-butt') Submit

        script(src="https://code.jquery.com/jquery-2.2.0.min.js")
        script(src="upload.js")

css样式位于

之下
#submit-butt{
    width:100px;
    height:auto;
    background-color:red;
    cursor:pointer;
    margin-left:150px;
}

#upload-butt{
    width:200px;
    height:auto;
    cursor:pointer;
    float:left;
}

h1{
    font-family:Impact;
}

jQuery代码如下所示

$('#upload-butt').on('click', function(){
   $('#upload-butt').on('change', function(){
      var file = $(this).get(0).files;

      if(file.length > 0){
          var formData = new FormData();
          formData.append('Upload', file, file.name);
          $.ajax({
              url: '/upload', 
              type: 'POST',
              data:formData,
              processData:false,
              contentType:false,
              success: function(data){
                  console.log('upload successful: '+data);
              }
          })
      }
   });
});

如何处理TypeError?如何将路径转换为字符串?我所拥有的是在通过强大的中间件运行后获得的解析数据。

1 个答案:

答案 0 :(得分:0)

更改' / upload'在这个代码的后端。不要使用req参数。不需要那样。

    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, '/uploads');

  // 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);

});