簡體   English   中英

Node.JS請求數據長度小於Content-Length中給出的長度

[英]Node.JS request data length is smaller than given in Content-Length

我的要求是多部分/表格數據

這就是我讀取請求數據的方式:

var data = "";

this.request.on( "data" , function( chunk ){

    data += chunk;

} );

this.request.on( "end" , function(){

    this.request.body = this.parseRequest( data );

    this.emit( "end" );

}.bind( this ) );

現在,請求的Content-Length25,981 ,但“結束”數據的長度為25,142

誰能解釋一下?

問題在這里:

data += chunk;

緩沖區中的.toString()方法損壞了二進制數據-這就是長度損失的來源。

兩種解決方案:

  1. 將塊保留為緩沖區並使用它。
  2. 使用.toString('binary') ,它將緩沖區轉換為沒有數據丟失的字符串(至少在我的測試中如此)。

我的代碼現在:

    var buffer = [];

    this.request.on( "data" , function( chunk ){
        buffer.push( chunk );
    } );

    this.request.on( "end" , function(){

        buffer = Buffer.concat( buffer );
        this.request.body = this.parseRequest( buffer.toString("binary") );
        this.emit( "end" );

    }.bind( this ) );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM