首页 文章

如何使用Image发送额外数据?

提问于
浏览
1

在图像库的情况下,图像包含 Headers ,描述和其他信息 . 我想在响应中传输每个信息(以及图像数据) . 这是可能的,在单个HTTP响应中 . 我问的原因是,在数据库中,在单个查询中,所有内容都可以返回即可 . 图像路径, Headers ,描述等 . 因此,在单个响应中返回所有内容是不利的 .
目前,我只能从数据库中获取图像路径和名称(写为imagepath)并将图像返回给用户 . 我的(通常的提取,读取和输出文件)后端代码(Php):

$img_id=$_GET['id'];
$con=mysqli_connect('localhost','username','password','mydb');
$stmt= mysqli_prepare($con,'select imagepath,description,title from images where imgid=?');
mysqli_stmt_bind_param($stmt,'s',$img_id)
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $imgpath,$desc,$title);
if (!mysqli_stmt_fetch($stmt)) return ;
//single result
if (file_exists($imgpath)) 
{
    header('Content-Type: image/png');
    header('Content-Length: ' . filesize($imgpath));
    readfile($file);
}

//目前,我无法通过此发送desc和title . 有没有办法在一个响应中发送它们?另外,有没有办法在浏览器端使用javascript检索它们?

1 回答

  • 0

    “X-”前缀用于非标准标头 .

    因此,您可以发送自己的 Headers ,其中包含您需要的信息:

    header('X-Image-Title: '.$title);
    

    更新:

    http://tools.ietf.org/html/rfc6648

    所以只需使用它而不使用 X- 前缀;)

相关问题