使用nodejs上传文件时出错

时间:2017-08-02 07:44:27

标签: javascript file-upload multer

我对此问题收到同样的错误:What type of error is this Error: ENOENT: no such file or directory, open

但是,我选择的所有类型的文件夹都会出现相同的错误,我确信我只选择了一个文件。我给出了这个错误

Error: ENOENT: no such file or directory, open 'uploads/userPhoto-1501659100410'

另外,要清楚,我的代码:

Server.js

var express =   require("express");
var multer  =   require('multer');
var app         =   express();
var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './uploads');
  },
  filename: function (req, file, callback) {
    callback(null, file.fieldname + '-' + Date.now());
  }
});
var upload = multer({ storage : storage}).single('userPhoto');

app.get('/',function(req,res){
      res.sendFile(__dirname + "/index.html");
});

app.post('/api/photo',function(req,res){
    upload(req,res,function(err) {
        if(err) {
            return res.end(err+" ");
        }
        res.end("File is uploaded");
    });
});

app.listen(3000,function(){
    console.log("Working on port 3000");
});

的index.html

<form id        =  "uploadForm"
     enctype   =  "multipart/form-data"
     action    =  "/api/photo"
     method    =  "post"
>
<input type="file" name="userPhoto" />
<input type="submit" value="Upload Image" name="submit">
</form>

另外我想知道我是否可以将.js文件中的文本放入脚本并将它们放在同一个jsp文件中。因为当我这样做时,我无法从中获得任何输出,并且我更改了我在eclipse中用作服务器的tomcat端口,但它说

The requested resource is not available.

0 个答案:

没有答案