简体   繁体   English

将图像转换为Data URI Base64时出现问题

[英]Issues converting image to Data URI Base64

I'm trying to convert an image located on a server to a Data URI. 我正在尝试将服务器上的图像转换为数据URI。 When I pass through the exact url location it converts fine. 当我通过确切的网址位置时,它可以很好地转换。 I need to be able to pass through the filename as a variable however when I do it returns with a base64 for data:text/html not data:image/jpeg. 我需要能够将文件名作为变量传递,但是当我执行时,它以base64返回data:text / html而不是data:image / jpeg。

Any help is appreciated! 任何帮助表示赞赏!

    function toDataURL(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
        var reader = new FileReader();
        reader.onloadend = function() {
            callback(reader.result);
        }
        reader.readAsDataURL(xhr.response);
    };
    xhr.open('GET', url);
    xhr.responseType = 'blob';
    xhr.send();
}

//Works Here
    toDataURL(`/attachments/HouseLandPackage/FloorPlans/Floorplan.jpg`, function(dataUrl) {
            UserSelection.FloorPlanBase64 = dataUrl;
        });

//Doesn't work here (Floorplan = Floorplan.jpg in this)
    toDataURL(`/attachments/HouseLandPackage/FloorPlans/${Floorplan}`, function(dataUrl) {
                UserSelection.FloorPlanBase64 = dataUrl;
            });

Try 尝试

toDataURL(`/attachments/HouseLandPackage/FloorPlans/${Floorplan}`, function(dataUrl) {
                UserSelection.FloorPlanBase64 = dataUrl;
            });

You should use ${variableName} instead of just {variableName} with backticks 您应该使用${variableName}而不是带有反引号的{variableName}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM