nodejs和上传文件时的表达错误,“无法读取未定义的属性” [英] nodejs and express error when uploading file, "cannot read property of undefined"

查看:98
本文介绍了nodejs和上传文件时的表达错误,“无法读取未定义的属性”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我看过其他与此类似的常见问题,但一直无法缓解我的问题。



目的是创建文件上传功能。前端如下所示:

 < div class =holdingDiv> 
< form action =/ file-uploadname =uploadclass =dropzone dz-clickableid =dropzoneAreaenctype =multipart / form-data>
< input style =display:nonetype =filename =thumbnail [thumbs]/>
< button id =uploadSubmitterclass =btn btn-primary btn-largetype =submit>已完成< / button>
< / form>
< / div>

我有表单类型和所有的爵士乐设置。



处理发布请求的server.js如下所示:

  app.post('/ file-上传',imports.upload); 

请注意,我还需要以下要求:

  //需要窗体
app.use(express.bodyParser());

以及调用imports.upload导出功能所需的功能。



exports.upload函数看起来像这样:

  exports.upload = function(req,res) {
console.log('FIRST TEST:'+ JSON.stringify(req.files));

console.log('second TEST:'+ req.files.thumbnail.thumbs.name);
//获取文件extenstion:
//console.log('size'+ req.files.thumbnail.size);
// console.log('test this:'+ req.files.thumbnail.name);
// var fileExtension = JSON.stringify(req.files);

//console.log('获取此文件类型:'+ fileExtension.name);
// console.log('this:'+ req.files.upload);

//fs.readFile(req.files.uploadFiles.path,function(err,data){
// // ...
// var newPath = __dirname +/uploads/\"+uploadFiles.name;
// fs.writeFile(newPath,data,function(err){
// res.redirect(back);
/ /});
//});
}

很多东西被注释掉,因为我正在尝试不同的方法来获得它上班。我可以使用JSON Stringify作为整个对象来调用它,但是我希望它可以作为一个对象,我可以从中获取信息,例如我想通过将它的名称分割为''来知道一个文件的类型。

  req.files.thumbnail.thumbs.name 

但是当我尝试这个(即使是JSON Stringyfied)它说它是未定义的。



我有TRIED的意思: p>

将整个函数移动到app.js(有一个使用req.body的小型登录功能,我认为这可能会修复它。



使用JSON Stringyfy来获取对象的特定部分(返回undefined)



将我的头撞在键盘上(返回undefined)



将表单更改为几种不同的东西,但是这里的大多数答案都表示表单数据是文件上传的最佳选择。



任何关于为什么会发生这种情况的帮助和指导将非常感谢!

解决方案

我不明白为什么你将输入的名称保持为缩略图[thumb]。
你必须在你的表单中添加方法=POST。



我将name属性更改为theFile,这里是html

 < div class =holdingDiv> 
< form action =/ file-uploadname =uploadclass =dropzone dz-clickableid =dropzoneAreaenctype =multipart / form-datamethod =post>
< input type =filename =theFile/>
< button id =uploadSubmitterclass =btn btn-primary btn-largetype =submit>已完成< / button>
< / form>
< / div>

现在在您的节点js服务器中执行此操作。

  app.post('/ file-upload',function(req,res){
console.log('FIRST TEST:'+ JSON.stringify(req。文件));
console.log('second TEST:'+ req.files.theFile.name);
fs.readFile(req.files.theFile.path,function(err,data){
var newPath =/home/path/to/your/directory/\"+req.files.theFile.name;
fs.writeFile(newPath,data,function(err){
res.send(hi);
});
});
});

req.files是一个json,提供上传的请求的详细信息。



希望这有帮助。


EDIT: === For clarity, I am looking to upload a file to the server, be it a picture or a bit of .txt ===

I've looked around at other common problems similar to this but have been unable to alleviate my problem.

The purpose is to create file upload functionality. The front end looks like this:

<div class="holdingDiv">
    <form action="/file-upload" name="upload" class="dropzone dz-clickable" id="dropzoneArea" enctype="multipart/form-data">
        <input style="display:none" type="file" name="thumbnail[thumbs]" />                      
        <button id="uploadSubmitter" class="btn btn-primary btn-large" type="submit">Finished</button>
    </form>
</div>

I have the form type and all that jazz setup.

The server.js that handles the post request looks like this:

app.post('/file-upload', imports.upload);

Note that I also have the following required:

//needed for forms
app.use(express.bodyParser()); 

aswell as funcionality necessary to call imports.upload exports function.

the exports.upload function looks a like this:

exports.upload = function (req, res) {
    console.log('FIRST TEST: ' + JSON.stringify(req.files));

    console.log('second TEST: ' +req.files.thumbnail.thumbs.name);
    //get the file extenstion:
    //console.log('size' + req.files.thumbnail.size);
  //  console.log('test this: ' + req.files.thumbnail.name);
   // var fileExtension = JSON.stringify(req.files);

    //console.log('Im getting this file type: '+ fileExtension.name);
   // console.log('this: '+req.files.upload);

    //fs.readFile(req.files.uploadFiles.path, function (err, data) {
    //    // ...
    //    var newPath = __dirname + "/uploads/"+uploadFiles.name;
    //    fs.writeFile(newPath, data, function (err) {
    //        res.redirect("back");
    //    });
    //});
}

A lot of stuff is commented out as I was trying different methods to get it to work. I can call it with JSON Stringify as a whole object, but I would like it as an object that I can dip into and get information from, for example I would like to know a files' type by splitting its name by '.':

req.files.thumbnail.thumbs.name

but when i try this (even JSON Stringyfied) it says it is undefined.

THINGS I HAVE TRIED:

Moving the whole function to app.js (there is a small login function that works using req.body, I assumed this may fix it.

using JSON Stringyfy to get at specific parts of the object. (returns undefined)

smashing my head against the keyboard. (returns undefined)

changing the form enctype to several different things, however most of the answers on here state that form-data is the best enctype for file uploads.

Any help and pointers as to why this is happening would be much appreciated!!

解决方案

I didn't understand why you kept the name of input as "thumbnails[thumb]". You have to add the method = "POST" to your form.

I changed the name attribute to "theFile" and here is the html

<div class="holdingDiv">
  <form action="/file-upload" name="upload" class="dropzone dz-clickable" id="dropzoneArea" enctype="multipart/form-data" method = "post">
    <input  type="file" name="theFile" />                      
    <button id="uploadSubmitter" class="btn btn-primary btn-large" type="submit">Finished</button>
  </form>
</div>

Now in your node js server do this.

app.post('/file-upload',function(req,res){
    console.log('FIRST TEST: ' + JSON.stringify(req.files));
    console.log('second TEST: ' +req.files.theFile.name);
    fs.readFile(req.files.theFile.path, function (err, data) {
        var newPath = "/home/path/to/your/directory/"+req.files.theFile.name;
        fs.writeFile(newPath, data, function (err) {
          res.send("hi");  
        });
    });
});

the req.files is a json which give the details of the uploaded request.

Hope this helps.

这篇关于nodejs和上传文件时的表达错误,“无法读取未定义的属性”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆