Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何在图像源中设置blob url_Html_Angular_Image_File_Blob - Fatal编程技术网

Html 如何在图像源中设置blob url

Html 如何在图像源中设置blob url,html,angular,image,file,blob,Html,Angular,Image,File,Blob,我从服务器返回一个二进制文件,并希望在前端显示它。我将二进制文件转换为blob,并使用DOM Sanitizer创建一个保存URL 问题是,当我将blob URL用于图像源时,找不到图像: 类型脚本: constblob=newblob([response],{type:'application/octetstream'}); const fileData=this.sanitizer.bypassSecurityTrustResourceUrl(window.URL.createObject

我从服务器返回一个二进制文件,并希望在前端显示它。我将二进制文件转换为blob,并使用DOM Sanitizer创建一个保存URL

问题是,当我将blob URL用于图像源时,找不到图像:

类型脚本:

constblob=newblob([response],{type:'application/octetstream'});
const fileData=this.sanitizer.bypassSecurityTrustResourceUrl(window.URL.createObjectURL(blob));
HTML:


来自服务器的响应

�PNG


IHDR��91tHiCCPICC ProfileH��WXS��[RIh�H ��R�K   �E�*�I �ĘD�ʢ�k���"��;��(v��P��u�`C�M
����������ϙs�S2��t�yRi.�@�$_����"u`���ǗK�qq���������r�E����=�P���8C ��A|��/��@�z�i�R%���&�T��ԸX�3ԸBe�ρxd�'�@�   �Y�,ȣ}bW�@,@�q _�@  try this:

 constructor(public _DomSanitizationService: DomSanitizer) {}

 ...

 <img [src]="_DomSanitizationService.bypassSecurityTrustUrl('data:image/png;base64,' + response)">
�巴布亚新几内亚
IHDR��91THICPICC剖面图��WXS��[下钻]�H��R�K�E�*�我�ĘD�ʢ�K���"��;��(五)��P��U�`C�M
����������ϙs�S2��T�伊里。�@�$_����"u`���ǗK�qq���������R�E����=�P���8C��A|��/��@�Z�我�R%���&�T��ԸX�3ԸBe�ρxd�'�@�   �Y�,ȣ}bW�@,@�q_�@   试试这个:

const blob = URL.createObjectURL(response);

const fileData  = this.sanitizer.bypassSecurityTrustUrl(blob);
constructor(public\u-domsanizationservice:domsanizizer){
...

您只需要响应中的createObjectURL

 const blob = 'data:image/png;base64,' + response;
  const fileData  = this.sanitizer.bypassSecurityTrustUrl(blob );
如果响应base64格式

     <input type="file" name="file" class="file"id="files"onchange="changefile()"/>

<video style="display: none;" id="vid"src="" width="350px" controls />



我只测试了视频,但它应该与图像

    document.querySelector("input[type=file]")
.onchange = function(event) {
  let file = event.target.files[0];
  let blobURL = URL.createObjectURL(file);
  document.querySelector("img").style.display = "block";
  document.querySelector("img").src = blobURL;

}

无法以这种方式创建blob变量。它显示:错误类型错误:无法对“URL”执行“createObjectURL”:未找到与提供的签名匹配的函数。能否在问题中发布响应数据?