我需要在文件上传时为大文件(.ISO)生成校验和。我正在使用ExpressJS / Connect通过POST(多部分)处理上传请求
//express setup app.js
app.use('/upload',express.bodyParser({
defer : true, //enable progress event
keepExtensions : true,
limit : app.locals.fileLimit,
uploadDir : path.join( __dirname, '..', 'tmp'),
hash : true //this doesnt appear to do anything
}));
//express route routes.js
app.post("/upload", function(req, res){
var fs = require('fs');
var format = require('util').format;
req.form.hash = true; //this doesnt return the hash for some reason
req.form.on('fileBegin', function(err) {
log.info("Starting new upload...");
});
req.form.on('file', function(name, file) {
fs.rename(file.path, req.form.uploadDir + "/" + file.name);
console.info(file);
});
req.form.on('error', function(err) {
log.error('error',err);
req.resume();
});
req.form.on('field', function(field, value) {
log.info("Passed Field (%s=%s)...",field,value);
});
//this part event never gets called for some reason but
//I imagine this is where you might do a hash update
req.form.on('part', function(part){
console.log('PART upload %s %s', part.name, part.filename);
var out = fs.createWriteStream('../tmpstrm/' + part.filename);
part.pipe(out);
});
req.form.on('progress', function(bytesReceived, bytesExpected) {
var percent = (bytesReceived / bytesExpected * 100) | 0;
console.info( "PROGRESS :"+ ((bytesReceived / bytesExpected)*100) + "% uploaded");
});
req.form.on('end', function() {
var fileInfo = format('\nuploaded %s (%d Kb) to %s as %s'
, req.files.upload.name
, req.files.upload.size / 1024 | 0
, req.files.upload.path
, req.body.title);
res.locals.fileInfo = fileInfo; //renders info in jade
res.render('media/index');
});
});
我找不到关于我的特定问题的太多信息;它可能不是一个典型的用例。但我发现的一个问题有how to update the hash on a data event
的简短例子var hash = crypto.createHash('sha1');
var data = // incoming file data
data.on('data', function(chunk){
hash.update(chunk);
});
data.on('end', function(){
var sha = hash.digest('hex');
// Do something with it
})
我是流媒体,大块等新手,因此我们非常感谢您的指导。
答案 0 :(得分:2)
在我的作者 - 摘要npm模块源代码中,您可能会发现一些示例代码:
但基本上你有两种方法:
允许bodyParser将HTTP请求主体流式传输到文件系统,然后通过从文件系统流式传输数据来计算校验和(以编写器摘要代码为例)。
您可以尝试通过将事件处理函数绑定到req
对象data
和end
事件来计算实际HTTP请求正文到达时的校验和。您应该能够在bodyParser将其流式传输到磁盘时执行此操作。只需要确保事件足够早,这样你就不会错过任何块。