【发布时间】:2014-07-15 12:11:34
【问题描述】:
我目前正在尝试通过 REST API 显示个人资料图片。 我让它工作,但我觉得这不是正确的方法。
所以,在 API 中我得到了一个控制器:
public function getUserprofilepicAction(Request $request) {
/**
* Handle stuff
*/
$file = $user->getWebPath();
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
}
现在 API 调用是这样的:
<somesite>/api/user/userprofilepic.json?userId=84
为了在网页上查看图片:
<img src = <somesite>/app_dev.php/api/user/userprofilepic.json?userId=84>
所以现在我想知道,关于通过 symfony2 中的 restAPI 发送/查看图片的最佳实践究竟是什么。
-干杯
【问题讨论】:
-
你应该只返回路径而不是文件,所以你返回路径并且 img-tag 读取文件
-
我尝试返回 webPath,即:“
\/UserBundle\/Entity\/..\/..\/..\/..\/profile\/uploads\ /pictures\/623697e0b5da3e25b8e8b83ad40918acc4f8ba73.jpeg" 但是图像不会显示,也许我把它放在 错误?
标签: symfony