0

想要达到的目的很简单。我正在使用角度全栈生成器来生成骨架。用户应该能够上传个人资料图片以及他们的姓名、电子邮件等。我正在使用angular-file-uplpoad发送多部分表单请求。根据它的 wiki,我还使用了下面的代码。

// Requires multiparty 
multiparty = require('connect-multiparty'),
multipartyMiddleware = multiparty(),

// Requires controller
UserController = require('./controllers/UserController');

// Example endpoint 
router.post('/api/user/uploads', multipartyMiddleware, UserController.uploadFile);
我还使用gridfs-stream将配置文件图像流式传输到 mongo gridfs。这里一切似乎都很好。因为如果我将配置文件图像流式传输到服务器本地文件中,我实际上可以打开并查看图像。问题是,现在我想将图像发送回浏览器。我在下面写了代码

var Grid = require('gridfs-stream');
var GridFS = Grid(mongoose.connection.db, mongoose.mongo);
var fs = require('fs');

/*
  var UserSchema = new Schema({
  first_name: String,
  last_name: String,
  email: { type: String, lowercase: true },
  role: {
    type: String,
    default: 'user'
  },
  hashedPassword: String,
  provider: String,
  salt: String,
  phone: String,
  projects: [{
    type : Schema.Types.ObjectId,
    ref : 'Project'
  }],
  profile_picture: Schema.Types.ObjectId
});
*/

// each user has a _id for a image file in mongodb
getFile : function() {
   
   var readstream = GridFS.createReadStream({
        _id : this.profile_picture,
   });
   response.writeHead(200, {'Content-Type': 'iamge/png' });
   readstream.pipe(response);
    
  },
但这不起作用。来测试它。我什至使用 fs.createReadStream(filename) 来加载存储在服务器端的静态图像。该图像实际上是发送的,但它是在浏览器中接收到的损坏的图像。我也试过 response.download('filename'); 还是不行。有什么建议么?

谢谢!

4

1 回答 1

0

您写道: {'Content-Type': 'iamge/png' } 将其修复为: {'Content-Type': 'image/png' }

让我知道这是否解决了它,因为我也有问题并且有类似的代码。

于 2015-05-07T20:40:20.287 回答