Busboy中的“字段”和“文件”事件之间有何区别? [英] What's the difference between the 'field' and the 'file' events in busboy?

查看:125
本文介绍了Busboy中的“字段”和“文件”事件之间有何区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Busboy 是我用来上传文件的中间件。使用Chrome内的html表单,我可以上传文件(使用 file事件),但是当android客户端尝试上传文件时,它不会触发 file事件,而是触发 field事件。



这是我在服务器端使用的代码段:

 从快递进口快递;来自 connect-busboy的
进口busboy;

const app = express();

const busUpload =(req,res)=> {
req.busboy.on('file',function(fieldname,file,filename,encoding,mimetype){
saveTo =`$ {destination} / $ {filename}`;

Log('uploading to',saveTo);
file.pipe(fs.createWriteStream(saveTo));
//文件成功保存。
});

req.busboy.on('field',function(key,value,keyTruncated,valueTruncated){
//我猜'value'包含文件,但是如何保存?文件名是什么?
});

req.busboy.on('finish',function(){
Log('uploadcomplete');
// res.writeHead(200,{'Connection' :'close'});
res.json({sucess:true});
});

// req.pipe(req.busboy);

};

app.use('/ uploads',busboy({immediate: true}),busUpload)



‍‍有什么区别?我该如何告诉Android开发人员更改其请求?或者如何将文件保存在字段事件的处理程序中?

解决方案

根据busboy文档,触发了 file 事件文件上传:



  • 为找到的每个新文件表单字段发出。 transferEncoding包含文件流的 Content-Transfer-Encoding值。 mimeType包含文件流的 Content-Type值。


由于得到 field 事件,我的猜测是输入的发送方式与html文件输入元素中的发送方式不同:

 <输入type = file name =文件名 accept =媒体/类型> 

我不熟悉android API,因此不确定如何发送文件,但是由于您的现场事件是触发您似乎应该深入代码的客户端(Android),看看您有什么可能。



或者,您可以验证字段输入是否包含类似您已经在上面的问题中的代码段中提出了建议:

  //我猜'value'包含文件,但是该怎么做我保存吗?文件的名称是什么? 

您可以通过调试/分析/记录请求对象来简单地检查从客户端获得的内容。 / p>




如果您无法自己编写客户端代码,也可以尝试构建一个小的html上传页面,您可以在其中将文件上传到服务器,并查看行为。这样,您将可以轻松检查服务器是否按预期工作。
在这个小型应用程序中,可以通过不同的方式上传文件:


  1. 通过表格形式,例如示例此处

  2. 二进制内容如示例此处

然后测试两种情况下服务器是否都能正确处理文件。


Busboy is the middleware I'm using to upload a file. Using an html form inside Chrome, I can upload files (using the 'file' event) but when an android client tries to upload a file, it doesn't trigger the 'file' event, it triggers the 'field' event instead.

Here the code snippet I am using on the server side:

import express from 'express';
import busboy from 'connect-busboy';

const app = express();

const busUpload = (req, res)=> {
    req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        saveTo = `${destination}/${filename}`;

        Log('uploading to', saveTo);
        file.pipe(fs.createWriteStream(saveTo));
        // file is saved successfully.
    });

    req.busboy.on('field', function(key, value, keyTruncated, valueTruncated) {
    //  I guess 'value' contains the file, but how do I save it? what is the name of file?
    });

    req.busboy.on('finish', function() {
        Log('upload completed');
        // res.writeHead(200, {'Connection': 'close'});
        res.json({sucess: true});
    });

    // req.pipe(req.busboy);

};

‍‍app.use('/uploads', busboy({immediate: true}), busUpload)

‍‍‍What's the difference? What should I tell the android developer to change in his request? Or how can I save the file inside the handler of the 'field' event?

解决方案

According to the busboy documentation the file event is triggered for file uploads:

  • Emitted for each new file form field found. transferEncoding contains the 'Content-Transfer-Encoding' value for the file stream. mimeType contains the 'Content-Type' value for the file stream.

Since you get a field event my guess would be that the input is not sent similarly as in an html file input element:

<input type="file" name="filename" accept="media/type">

I am not familiar with android API so not sure how files are sent, but since your field event is triggered it seems you should dive into the client side (Android) of the code and see what possibilities you have.

Alternatively you can validate whether your field input contains a file like you already suggested in the code snippet in your question above:

//  I guess 'value' contains the file, but how do I save it? what is the name of file?

You can simply check what you get from your client by debugging/analyzing/logging your request object.


If you don't have the possibility to work on the client side code yourself, you could also try building a small html upload page where you upload files to your server and see what behavior you get. Like that you will be able to check easily if your server is working as expected. In this small application could upload files in different ways:

  1. through a form as in the example here
  2. as binary content as in the example here

And test whether your server is able to handle the file correctly in both cases.

这篇关于Busboy中的“字段”和“文件”事件之间有何区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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