简体   繁体   English

数据URI导致Javascript中的语法错误

[英]Data URIs causing syntax errors in Javascript

I'm trying to draw an image inside a canvas element using a data URI. 我正在尝试使用数据URI在canvas元素内绘制图像。 I'm using Django and passing a variable containing the base64 encoding of the photo to the template. 我正在使用Django,并将包含照片的base64编码的变量传递到模板。 In the past, I have used that like so: 过去,我曾经这样使用过:

<img src="data:image/jpg;base64, {{ photo_data }}">

This works fine. 这很好。 I now want to replace that with this: 我现在要替换为:

<canvas id="canvas"></canvas>

and use javascript to draw the image in the canvas. 并使用javascript在画布上绘制图像。 I tried out the code from this answer : 我从这个答案中尝试了代码:

var canvas = $('#canvas');
var ctx = canvas.getContext('2d');
var img = new Image;
$(img).load(function(){
    ctx.drawImage(img, 0, 0);
});
img.src = "data:image/jpg;base64, {{ photo_data }}"

This gives me the javascript error "SyntaxError: unterminated string literal" on the last line. 这使我在最后一行出现了javascript错误“ SyntaxError:未终止的字符串文字”。 I read that this error usually happens because of line breaks. 我读到这个错误通常是由于换行而发生的。 Upon inspecting the page source it looks like there are line breaks in there: 检查页面源代码后,看起来好像有换行符:

在此处输入图片说明

So then I tried removing line breaks: 因此,我尝试删除换行符:

function removeBreaks(data){
    return data.replace(/(\r\n|\n|\r)/gm, "");
}

var photo_data = removeBreaks({{ photo_data }});

Now I'm getting a new error when I call removeBreaks. 现在,我在调用removeBreaks时遇到了一个新错误。 "SyntaxError: identifier starts immediately after numeric literal". “ SyntaxError:标识符在数字文字之后立即开始”。

var photo_data = removeBreaks(/9j/4AAQSkZ...
----------------------------------^

Is there some sort of escaping I need to do, and if so how would I do it? 我需要做某种转义吗,如果可以,我该怎么做?

Update 更新

I tried all of the suggestions from the comments and the answer. 我尝试了评论和答案中的所有建议。 So far the thing that has come closest to working is doing 到目前为止,最接近工作的事情正在做

photo_data = json.dumps(<base64 encoded image file>)

on the Django side and 在Django方面,

var photo_data = "{{ photo_data }}".replace(/&quot;/g,"");
img.src = "data:image/jpg;base64, " + photo_data;

on the Javascript side. 在JavaScript方面。 The image doesn't show at all if I don't replace the &quot; 如果我不替换&quot; s, and when I do remove them only the top third of the image shows. s,当我删除它们时,仅显示图像的前三分之一。 I tried resizing the canvas element, that doesn't seem to be the issue. 我尝试调整canvas元素的大小,但这似乎不是问题。

Update #2 更新#2

Turns out the canvas size was the issue. 原来画布大小是问题所在。 Width and height of the canvas element need to be as big or bigger than the width and height of the image, which I wasn't setting when I created the Image object. canvas元素的宽度和高度必须大于或等于图像的宽度和高度,而这在创建Image对象时并未设置。 Both the accepted answer and json.dumps (with the &quot; caveat) work to solve the original problem. 接受的答案和json.dumps(带有&quot;警告”)都可以解决原始问题。

I do it like this: 我这样做是这样的:

img.src = "1234 ... 56789" + //
   "01234567 ... 7890" + //
   .....
   "67879878907890";

Some people I work with like to put the + on the front of the line, claiming its easier to read. 与我一起工作的某些人喜欢将+放在行的最前面,声称+易于阅读。

Assume you have the image data, base64 encoded in a string. 假设您具有以字符串形式编码的base64图像数据。 A sort of pseudo C/Java/Javascript like language would have code something like this (since I don't know Python): 某种伪C / Java / Javascript之类的语言将具有如下所示的代码(因为我不了解Python):

String image64 = ...
String output = "img.src = \"data:image/jpg;base64, ";

for (int i = 0; i < image64.length ; i += 100) {
    if (i > 0) {
        output += "\" + // \n    \"";
    }
    if (i + 100 < image64.length) {
        output += image64.substring(i, i+100);
    } else {
        output += image64.substring(i); // this gets the rest
    }
}
output += "\";\n";

Then you write the value of output to where ever you are putting your javascript. 然后,将output值写入放置JavaScript的位置。

Now a quick look a Django and Python stuff leads me to suspect it would look something like this in the template: 现在快速浏览一下Django和Python的东西,就让我怀疑它在模板中看起来像这样:

img.src = "data:image/jpg;base64, " + //
{% for line in photo_data_lines %}
    "{{ line }}" + //
{% endfor %}
    "";

But you'd have to set up the photo_data_lines array with 100 character chunks of the image data in each element beforehand. 但是,您必须事先在每个元素中设置photo_data_lines数组,其中包含100个字符的图像数据块。 (Or however long you want the chunks.) (或者不管您想要多长时间。)

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

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