微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 从AWS S3加载图像时获取响应标头

their recommendation for storing metadata之后,我将图像存储在S3上,其中描述存储在元数据中

如何直接在浏览器中显示图像时如何检索响应标头?我试过在img元素上查看onload事件但找不到头文件.我也尝试过XMLHttpRequest,它在响应中获取标题,但我不能将responseText用作img src.

解决方法:

最终我找到了this fiddle并通过XMLHttpRequest获取了图像,然后将头文件中的desc设置为自定义属性中的图像:

function(image_path, img){ 
    // Use a native XHR so we can use custom responseType
    var xhr = new XMLHttpRequest();
    xhr.open("GET", image_path, true);

    // Ask for the result as an ArrayBuffer.
    xhr.responseType = "arraybuffer";

    xhr.onload = function( e ) {
        // Obtain a blob: URL for the image data to draw it
        var arrayBufferView = new Uint8Array( this.response );
        var blob = new Blob( [ arrayBufferView ], { type: "image/jpeg" } );
        var imageUrl = URL.createObjectURL( blob );
        img.src = imageUrl;

        // Get the description from S3 Metadata
        var desc = this.getResponseHeader('x-amz-Meta-description');
        img.setAttribute('data-description', desc);
    };
    xhr.send();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐