使用ajax时,没有从文本到arraybuffer的转换

时间:2016-04-22 08:30:59

标签: javascript jquery ajax base64

我尝试使用ajax获取文件,并使用虚拟标记添加下载数据URI。我正在使用此库jquery.binarytransport.js

当我尝试使用binary或arraybuffer选项获取pptx文档时:

$.get(url, function(data) {
    var dataURI = 'data:application/octet-stream;base64,' + btoa(data);
}, 'binary');

$.get(url, function(data) {
    var dataURI = 'data:application/octet-stream;base64,' + btoa(data);
}, 'arraybuffer');

我发现错误"No conversion from text to binary""No conversion from text to arraybuffer"与blob相同。

当我将类型设置为文本时,当我在响应中使用btoa函数时出现错误:"Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range."

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我通过使用text作为responseType并使用此函数来解决问题:

uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
uint8_t *original = "aaaaaaaaaaaaaaaaa65aaaaaaaaaaaassssssssssssssssssssss65sssaaaaaa";
int len = 65;
uint8_t out[64] = {0,}; 

uint8_t ctext[200] = { 0, };
//uint8_t *ctext = (uint8_t*)malloc(sizeof(uint8_t) * len);

for (int i = 0; len > 0; i+=64) {
    len = len - 64;     
    AES128_CBC_encrypt_buffer(out, original + i, 64, key, iv);
    memcpy(ctext+i , out, 64);
}
uint8_t ptext[200] = { 0, };
len = 65;
for (int i = 0; len > 0; i += 64) {
    len = len - 64;  
    AES_CBC_dec(out, ctext + (i * sizeof(uint8_t)), key, iv, 64);
    memcpy(ptext + (i * sizeof(uint8_t)), out, 64);
}
return 0;