express的实例:
var svgCaptcha = require(‘svg-captcha’); app.get(’/captcha’, function (req, res) { var captcha = svgCaptcha.create(); req.session.captcha = captcha.text; res.type(‘svg’); res.status(200).send(captcha.data); });
egg可以在控制器中这样写有问题吗???,无法返回图片:
class HomeController extends Controller { async index() { await this.ctx.render(‘index.html’); } async getImage() { var captcha =await svgCaptcha.create(); console.log(captcha.text); console.log(captcha); this.ctx.type(‘svg’); await this.ctx.status(200).send(captcha.data); } } module.exports = HomeController;
路由文件:
router.get('/getImage', controller.home.getImage);
captcha打印出来是个啥?
egg中这样写直接访问图片链接可以返回图片,但是如果在网页中src标签中引入无法获取图片: async getImage() { var captcha = svgCaptcha.create(); // req.session.captcha = captcha.text; console.log(captcha.text); console.log(captcha); this.ctx.body=captcha.data;
}
@IEfucker { text: ‘O4Tc’, data: '‘svg二进制数’}
问题解决了,处理方法: 参考: https://github.com/lemonce/svg-captcha/issues/25