我在使用GridFS正确存储和检索文件时遇到了一些问题。目前,如果我提交.txt文件,我最终会收到该文件的内容,但如果我提交.doc文件,我会收到一堆乱码(如带有问号的blackdiamonds)。
我的最终目标是能够提交文件,然后允许某人稍后根据其他请求下载该文件。
编写代码:
router.post('/jobs/listing/:job/apply', multipartyMiddleware, function(req, res, next){
var myFile = req.files.file;
var conn = mongoose.createConnection('mongodb://localhost/test');
conn.once('open', function () {
var gfs = Grid(conn.db, mongoose.mongo);
var readfile = fs.createReadStream(myFile.path);
var f = readfile.pipe(gfs.createWriteStream({
filename: myFile.name
}));
f.on('close', function(){
console.log('File Added to GRIDFS');
res.end();
});
});
}
阅读代码:
var conn = mongoose.createConnection('mongodb://localhost/test');
conn.once('open', function () {
var gfs = Grid(conn.db, mongoose.mongo);
var readstream = gfs.createReadStream({
filename: req.file //set to desired filename
});
var f = readstream.pipe(res);
});
有什么建议吗?我非常感谢您提供的任何帮助。感谢。
编辑:问题与角度上传问题有关。
答案 0 :(得分:0)
这是我从其他开发人员复制并修改的简单实现。这对我有用:
https://gist.github.com/pos1tron/094ac862c9d116096572
eb deploy
Creating application version archive "v0_9_2-78-g5ed5".
Uploading ujoin-www/v0_9_2-78-g5ed5.zip to S3. This may take a while.
Upload Complete.
INFO: Environment update is starting.
INFO: Deploying new version to instance(s).
ERROR: Script /opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh failed with returncode 18
ERROR: [Instance: i-2ad238d5 Module: AWSEBAutoScalingGroup ConfigSet: null] Command failed on instance. Return code: 1 Output: Error occurred during build: Command hooks failed
.
INFO: Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].
INFO: New application version was deployed to running EC2 instances.
ERROR: Update environment operation is complete, but with errors. For more information, see troubleshooting documentation.
ERROR: Update environment operation is complete, but with errors. For more information, see troubleshooting documentation.
vipul$ eb deploy --debug
2015-05-11 11:06:27,291 (DEBUG) eb : logging initialized for 'eb' using LoggingLogHandler
2015-05-11 11:06:27,291 (DEBUG) cement.ext.ext_plugin : plugin config dir /etc/eb/plugins.d does not exist.
2015-05-11 11:06:27,292 (DEBUG) cement.ext.ext_plugin : plugin config dir /Users/vipul/.eb/plugins.d does not exist.
2015-05-11 11:06:27,294 (DEBUG) eb : collecting arguments/commands for <ebcli.core.base.EbBaseController object at 0x10e633950>
2015-05-11 11:06:27,298 (DEBUG) eb : collecting arguments/commands for <ebcli.controllers.deploy.DeployController object at 0x10e6387d0>
-- EBCLI Version: 3.4
-- Python Version: 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)]
2015-05-11 11:06:27,304 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:27,514 (DEBUG) ebcli.objects.sourcecontrol : Git Version: git version 2.2.0
2015-05-11 11:06:27,754 (DEBUG) ebcli.objects.sourcecontrol : git symbolic-ref result: refs/heads/master
2015-05-11 11:06:27,755 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:27,758 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:27,761 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:27,771 (DEBUG) ebcli.objects.sourcecontrol : Git Version: git version 2.2.0
2015-05-11 11:06:27,777 (DEBUG) ebcli.objects.sourcecontrol : git symbolic-ref result: refs/heads/master
2015-05-11 11:06:27,777 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:27,780 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:27,783 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:27,786 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:27,790 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:27,802 (DEBUG) ebcli.objects.sourcecontrol : Git Version: git version 2.2.0
2015-05-11 11:06:27,812 (DEBUG) ebcli.objects.sourcecontrol : git symbolic-ref result: refs/heads/master
2015-05-11 11:06:27,813 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:27,818 (DEBUG) ebcli.lib.aws : Creating new Botocore Session
2015-05-11 11:06:27,818 (DEBUG) ebcli.lib.aws : Botocore version: 0.93eb
2015-05-11 11:06:27,820 (DEBUG) ebcli.lib.aws : Creating new Botocore Client for elasticbeanstalk
2015-05-11 11:06:28,013 (DEBUG) ebcli.lib.aws : Successfully created session for elasticbeanstalk
2015-05-11 11:06:28,013 (INFO) eb : Deploying code to ujoin-stage-green in region us-east-1
2015-05-11 11:06:28,013 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:28,029 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:28,233 (DEBUG) ebcli.objects.sourcecontrol : git diff --numstat result: with errors:
2015-05-11 11:06:28,233 (INFO) eb : Getting version label from git with git-describe
2015-05-11 11:06:28,505 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:28,509 (DEBUG) ebcli.core.fileoperations : Project root found at: /Users/vipul/Sites/ujoin-www
2015-05-11 11:06:28,509 (DEBUG) ebcli.lib.elasticbeanstalk : Inside get_application_versions api wrapper
2015-05-11 11:06:28,510 (DEBUG) ebcli.lib.aws : Making api call: (elasticbeanstalk, describe_application_versions) to region: us-east-1 with args:{'ApplicationName': 'ujoin-www'}
答案 1 :(得分:0)
对于任何最终解决此问题的人, 我有相同的症状,问题是中间件。这是一个粗糙的错误。 connect-livereload正在破坏响应。