我正在使用 node.js 为 Azure 文件存储创建文件服务。我正在使用azure-storage-node (http://azure.github.io/azure-storage-node/)
这个。
我正在尝试从 Azure 文件存储下载文件。下面是我的代码片段
// Download a file from Share
exports.get = function(request, response){
var shareName = request.headers.sharename;
var dirPath = request.headers.directorypath;
var fileName = request.headers.filename;
var fileService = azure.createFileService();
var readStream = fileService.createReadStream(shareName, dirPath, fileName);
var dataLength = 0;
var body = '';
readStream.on('data', function (chunk) {
dataLength += chunk.length;
})
readStream.on('end', function(){
console.log('The length was:', dataLength);
});
response.setHeader('Content-Type', 'application/json');
response.send(statusCodes.OK, JSON.stringify("Success!"));
}
我能够获得数据流。但是我怎样才能在响应中发送流,以便我们可以在 rest 调用中获取它。
我试过readStream.pipe(response);
了
response.write(typeof chunk);
response.end() but it doesnt work;
我是 node.js 的新手。请帮助我。
更新:
我尝试了以下。
response.writeHead(200, {'Content-Type': 'application/json'});
var readStream = fileService.createReadStream(shareName, dirPath, fileName);
readStream.pipe(response);
但它抛出以下错误。
ERROR
An unhandled exception occurred. Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:679:11)
at ServerResponse.res.setHeader (D:\home\site\wwwroot\node_modules\express\node_modules\connect\lib\patch.js:59:22)
at ServerResponse.res.set.res.header (D:\home\site\wwwroot\node_modules\express\lib\response.js:518:10)
at addDefaultHeaders (D:\home\site\wwwroot\runtime\request\requesthandler.js:582:9)
at ServerResponse.<anonymous> (D:\home\site\wwwroot\runtime\request\requesthandler.js:291:13)
at ServerResponse._.wrap [as end] (D:\home\site\wwwroot\node_modules\underscore\underscore.js:692:22)
at ChunkStream.onend (stream.js:66:10)
at ChunkStream.EventEmitter.emit (events.js:126:20)
at ChunkStream.end (D:\home\site\wwwroot\App_Data\config\scripts\node_modules\azure-storage\lib\common\streams\chunkstream.js:90:8)
at Request.onend (stream.js:66:10)
的返回数据类型 fileService.createReadStream(shareName, dirPath, fileName);
是ChunkStream
更新:
这是我更新的有效代码。
var option = new Object();
option.disableContentMD5Validation = true;
option.maximumExecutionTimeInMs = 20 * 60000;
option.timeoutIntervalInMs = 20 * 6000;
fileService.getFileToStream(shareName, dirPath, fileName, response, option, function(error, result, res) {
if(!error) {
if(res.isSuccessful) {
console.log(result);
console.log(res);
console.log("Success!");
}
}
});
但更频繁地,我遇到了错误。
ERROR
An unhandled exception occurred. Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:679:11)
at ServerResponse.res.setHeader (D:\home\site\wwwroot\node_modules\express\node_modules\connect\lib\patch.js:59:22)
at ServerResponse.res.set.res.header (D:\home\site\wwwroot\node_modules\express\lib\response.js:518:10)
at addDefaultHeaders (D:\home\site\wwwroot\node_modules\azure-mobile-services\runtime\request\requesthandler.js:590:9)
at ServerResponse. (D:\home\site\wwwroot\node_modules\azure-mobile-services\runtime\request\requesthandler.js:299:13)
at ServerResponse._.wrap as end
at Request.onend (stream.js:66:10)
at Request.EventEmitter.emit (events.js:126:20)
at IncomingMessage.Request.onRequestResponse.strings (D:\home\site\wwwroot\App_Data\config\scripts\node_modules\azure-storage\node_modules\request\request.js:1153:12)
at IncomingMessage.EventEmitter.emit (events.js:126:20)