上传HUGE文件> 2GB到nodejs(n Firefox) [英] Upload HUGE file > 2GB to nodejs (n Firefox)
问题描述
pre $
var http = require(http);
var url = require(url);
$ b $ http.createServer(function(request,response){
switch(url.parse(request.url).pathname){
case/:
displayForm(request,response);
break;
case/ upload:
response.writeHeader(200,{Content-Type:text / html});
response.end(
'< h1> File Uploaded!< / h1>'
);
break;
}
})。listen 1234);
var displayForm = function(request,response){
response.writeHeader(200,{Content-Type:text / html});
response.end(
'< form action =/ uploadmethod =postenctype =multipart / form-data>'+
'< input type = filename =uploadFile>'+
'< input type =submitvalue =Upload!>'+
'< / form>'
);
};
编辑:我刚刚意识到它只发生在 Firefox ,而不是Chrome浏览器!
对于这样的大文件,我推荐你用 FileReader
,用小于code> Blob 或 Blobbuilder
Socket.io)按进度条块连接文件块,连接服务器上的块。当你发送完最后一个数据块时,你的后端就会得到完整的文件。
你也可以在cookie / LocalStorage中保存当前数据块号码,以便在连接中断的情况下继续传输数据。 / p>
PS对于古老的IE和其他狗屎,有使用Blob,谷歌它的Flash插件。
$ b
var http = require("http");
var url = require("url");
http.createServer(function (request, response) {
switch(url.parse(request.url).pathname) {
case "/":
displayForm(request, response);
break;
case "/upload":
response.writeHeader(200, {"Content-Type":"text/html"});
response.end(
'<h1>File Uploaded!</h1>'
);
break;
}
}).listen(1234);
var displayForm = function(request, response) {
response.writeHeader(200, {"Content-Type":"text/html"});
response.end(
'<form action="/upload" method="post" enctype="multipart/form-data">' +
'<input type="file" name="uploadFile">' +
'<input type="submit" value="Upload!">' +
'</form>'
);
};
EDIT: I just realized it only happens in Firefox, not in Chrome!
For such large files I recommend you to read your file with FileReader
, chunk it on small binary pieces with Blob
or Blobbuilder
and transfer (by Socket.io) the file chunk by chunk with progress bar, concatenating the chunks on the server. When last chunk is sent - you got full file on your backend.
You can also save current chunk number in cookie/LocalStorage in case of connection breaks to continue transfer.
P.S. For ancient IE's and other shit there is flash plugin for using Blobs, google it.
P.S.S Max file size by POST requests for different browsers: http://motobit.com/help/scptutl/pa98.htm
这篇关于上传HUGE文件> 2GB到nodejs(n Firefox)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!