简体   繁体   English

JavaScript:从图像中删除字节(base64字符串)

[英]JavaScript: Remove bytes from image (base64 String)

I am working with Northwind service in SAP Web IDE. 我正在使用SAP Web IDE中的Northwind服务。 Images in this service is stored in base64 String format: FRwvAAIAAAAN.... . 此服务中的图像以base64字符串格式存储: FRwvAAIAAAAN....

I found out that I can't use these images in my app directly, using given base64 String value, because Northwind DB is old and made in MS Access and there are 78 redundant bytes which represent OLE header. 我发现我无法使用给定的base64 String值直接在我的应用程序中使用这些图像,因为Northwind DB很旧,是在MS Access中制作的,并且有78个冗余字节表示OLE标头。 So I would like to remove these 78 bytes from base64 String. 所以我想从base64字符串中删除这78个字节。

Can you please help me, using JavaScript language (I am new in this language). 您能使用JavaScript语言(我是该语言的新手)来帮助我。 I hope for you experts. 希望对您有帮助。 Here is what I have done: 这是我所做的:

I created function: 我创建了函数:

photo : function (value) { var str = ""; for (var p in value) { if (value.hasOwnProperty(p)) { str += value[p]; } } ..........

With this function I am taking base64 Sting as import parameter. 通过此功能,我将base64 Sting用作导入参数。 I converted that import parameter from object to string. 我将导入参数从对象转换为字符串。

So what should I do next? 那我下一步该怎么办? Create Array or something else? 创建数组还是其他? How can I remove 78 BYTES from String? 如何从字符串中删除78个字节?

In base64 each character contains six bits of information, so four characters contains 24 bits of information, which is three bytes. 在base64中,每个字符包含六位信息,因此四个字符包含24位信息,即三个字节。

You are in luck. 你真幸运。 As 78 happens to be evenly divisble by three, the first 78 bytes corresponds exactly to the first 104 characters (78 bytes = 624 bits = 104 characters). 由于78恰好可以被三等分,因此前78个字节正好对应于前104个字符(78个字节= 624位= 104个字符)。

So, to remove the first 78 bytes of a base64 string, you remove the first 104 characters: 因此,要删除base64字符串的前78个字节,请删除前104个字符:

s = s.substr(104);

(If you hadn't been so lucky, you would have had to decode the entire string into bytes, remove the first 78 bytes, the encode the bytes into a string again.) (如果不是很幸运,则必须将整个字符串解码为字节,删除前78个字节,然后再次将字节编码为字符串。)

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

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