2017-08-26 77 views
0

我有以下代碼,它的工作原理,但不顯示消息'太大的文件'。我看到一個ERR_CONNECTION_RESET的鉻屏幕。如何關閉連接並顯示消息?關閉連接,但不顯示消息

var size = 0; 

var gotData = function(d) { 
    size += d.length; 
    if (size > (1 * 1024 * 1024)) { 
      console.log('aborting request'); 
      res.writeHead(413, { 
       'Connection': 'close' 
      }); 
      res.end('too big file'); 
    } 
}; 

req.on('data', gotData); 

回答

0

使用這個。

var size = 0; 

var sizeTooMuch = function(){ 
     console.log('aborting request'); 
     response.writeHead(413, 'Request Entity Too Large', {'Content-Type': 'text/html'}); 
     response.end('<!doctype html><html><head><title>413</title></head><body>Too big file 413</body></html>')   
     return false; 
} 

var gotData = function(d) { 
    size += d.length; 
    if(size > (1 * 1024 * 1024){ 
    sizeTooMuch(); 
    } 
}; 

var endFunc = function(){ 
     console.log("got the file"); 
     res.send("got the file"); 
     res.end(); 
} 

req.on('data', gotData); 

req.on('end', endFunc); 
+0

謝謝,這工作,但服務器將加載整個上傳。我想中止並顯示消息。 –

+0

再次更新@MarioLopes – turmuka

+0

連接關閉,但我看到chrome頁面的ERR_CONNECTION_RESET。消息「太大的文件」不顯示。 –