所以我有一個代碼,圖像編碼到Base64一個..如何刪除base64編碼圖像中的開始字符串?
function toDataUrl(url, callback) {
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
callback(reader.result);
}
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.send();
}
toDataUrl('img/no_image_icon.png', function(base64Img) {
console.log(base64Img); // remove the data:image/png;base64, in the beginning
});
此代碼的工作,雖然我想刪除data:image/png;base64,
在生成base64Img
的開始。另外,如何在功能外訪問它? Like example
toDataUrl('img/no_image_icon.png', function(base64Img) {
var accessOutside = base64Img; // remove the data:image/png;base64, in the beginning
});
var newVariable = accessOutside;
任何幫助將不勝感激。
[如何返回來自異步調用的響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323 #14220323)你爲什麼要刪除該標題? – Thomas
@Thomas因爲在我的前端代碼(角)我已經有一個 – FewFlyBy
第一:如果你已經使用角,看看[它的承諾](https://docs.angularjs.org/api/ng /服務/ $ q)*(承諾一般)*,和[我已經鏈接的線程](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from -an-asynchronous-call/14220323#14220323),這會幫助你解決異步問題。第二:你說,你在你的前端代碼中添加頭文件?不要這樣做,那兩個部分緊密地結合在一起。因此,您的前端只能使用base64編碼的PNG,而不是任何圖像的URL。你看到這是如何限制? – Thomas