當我做一個POST請求的路線,我有又將圖像的二進制數據到img標籤
/generate/image
我得到這樣的:var文件=
����JFIF��C��C��� ��
�����+�}Yϭ�F39M>���������>���;��ˋ��uXʽ�w�ڤx\-[2g��k�S���H���m
[�V?[_W����#��v��}6�[��F�F�%����n�...
在客戶端
我做的:
var blob = new Blob([file], {type: 'image/png'});
var reader = new FileReader();
reader.onload = function (e) {
$('#result').attr('src', e.target.result);
};
reader.readAsDataURL(blob);
,但我得到一個腐敗的圖像
我該怎麼辦?
編輯: 如果我做
img.src = 'data:image/png;base64,' + btoa(file);
我得到:
Uncaught InvalidCharacterError: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
Base64編碼它,並使用[數據URI方案](https://en.wikipedia.org/wiki/Data_URI_scheme) – naomik
'getElementById'不'getElementByID'。 'attr'是一個jQuery函數,而不是JavaScript函數。 – Xufox
Xufox對不起,我有jQuery包括,我確實改變了getElementById,這是一個錯字 – AbdulHamid