简体   繁体   English

Nodejs:为什么我们需要传递一个 object FormData 以便 multer 可以正常工作(上传文件我使用 ajax jquery )

[英]Nodejs : Why we need to pass an object FormData so that multer can work well ( upload file I using ajax jquery )

i'm learning nodejs and using multer, ajax jquery for uploaded file, but I feel a little confused.我正在学习nodejs并使用multer,ajax jquery上传文件,但我觉得有点困惑。 Assume i have this code in html file假设我在 html 文件中有此代码

  <form action="/" method='post' enctype="multipart/form-data" id='form-data'>
    <input type="file" name='file' id='file' class='file'>
  </form>

and the script:和脚本:

<script>
    $(document).on('change', '#file', function () {
        var property = document.getElementById('file').files[0];
        var form_data = new FormData();
        form_data.append('file',  property);
        $.post({
            url: '/',
            type: 'post',
            data: form_data,
            contentType: false,
            cache: false,
            processData: false,
            success: function (data) {
                console.log(data);
            }
        })
    })

</script>

In the server.js ( server side ) i have this code:在 server.js (服务器端)我有这个代码:

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads')
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + path.extname(file.originalname))
  }
 })

var upload = multer({ storage: storage })


app.post('/', upload.single('file'), (req,res) =>{
  console.log(req.body);
  console.log(req.file);
})

This code run smoothly.这段代码运行流畅。 But this questions that bother me:但是这个困扰我的问题:

In the script, line 9, if i using "data: property" instead of "data: form_data", it doesn't work anymore, req.file is empty.在脚本第 9 行,如果我使用“数据:属性”而不是“数据:form_data”,它不再起作用,req.file 为空。 Why can't we directly pass variable "property" into data but must use a object FormData to do it?为什么我们不能直接将变量“属性”传递给数据,而必须使用 object FormData 来做到这一点?

Multer is designed to parse data encoded using the multipart/form-data format (which consists of any number of bits of data (such as files and pieces of text) separated with boundary markers). Multer 旨在解析使用multipart/form-data格式编码的数据(由任意数量的数据位(例如文件和文本片段)组成,并用边界标记分隔)。

The FormData API lets you collect different pieces of data, including files, and generate multipart/form-data with them. FormData API 可让您收集不同的数据,包括文件,并使用它们生成multipart/form-data

If you pass jQuery the value of property directly then you will be sending the file, which might be text/plain or image/png or just about anything, without any further encoding.如果您直接传递 jQuery property的值,那么您将发送文件,可能是text/plainimage/png或几乎任何东西,没有任何进一步的编码。 It is highly unlikely to be multipart/form-data since people don't generally store data in that format.它极不可能是multipart/form-data ,因为人们通常不会以这种格式存储数据。

You can write server side code that expects a raw file, but if you did that then you wouldn't be using Multer.您可以编写需要原始文件的服务器端代码,但如果您这样做了,那么您将不会使用 Multer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM