html5二进制传输,编写二进制数据html5 FileWriter(Writing binary data html5 FileWriter)

编写二进制数据html5 FileWriter(Writing binary data html5 FileWriter)

我在应用程序中使用webgl并将某些内容渲染到屏幕外渲染目标(帧缓冲区),然后执行readPixels。 获取像素数据后,我使用javascript jpeg编码器将其转换为jpeg:

我得到原始二进制jpeg数据,我想写入本地文件系统。 这是我用于编写的代码:

root.getFile(filename, {create: true}, function(fileEntry)

{

fileEntry.createWriter(function(writer)

{

writer.onwriteend = function(e)

{

System.debug("Write done");

};

writer.onerror = function(e)

{

System.error(e);

}

var data = new Blob(jpegData, { type: "image/jpeg" });

writer.write(data);

jpegData = null;

pixels = null;

uoozo.core.write[filename] = false;

});

},

function(e)

{

System.error(e);

});

但是,我得到了奇怪的结果。 当我把它放到图像元素时,jpegData很好,但是当我创建blob时,例如jpegData的大小为3200字节,但创建的Blob总是4818字节,FileWriter在写操作后也返回相同的位置。 创建的jpeg显然是错误的,不会打开。 我不明白我怎么能让FileWriter只是将darn二进制数据写入文件而不试图聪明(或愚蠢)。

有人可以帮我吗? 谢谢。

I am using webgl in an application and render something to an offscreen render target (frame buffer) and then do a readPixels. After getting the pixel data I convert it to jpeg using javascript jpeg encoder available here:

I get raw binary jpeg data that I want to write to a local file system. Here is the code that I am using for writing:

root.getFile(filename, {create: true}, function(fileEntry)

{

fileEntry.createWriter(function(writer)

{

writer.onwriteend = function(e)

{

System.debug("Write done");

};

writer.onerror = function(e)

{

System.error(e);

}

var data = new Blob(jpegData, { type: "image/jpeg" });

writer.write(data);

jpegData = null;

pixels = null;

uoozo.core.write[filename] = false;

});

},

function(e)

{

System.error(e);

});

However, I get weird results. jpegData is fine when I put it to an image element however when I create the blob e.g. jpegData is 3200 bytes in size but the Blob created is always 4818 bytes and FileWriter also returns the same position after the write operation. The jpeg created is obviously wrong and doesn't open. I don't understand how I can get the FileWriter to just write the darn binary data into the file without trying to be clever (or stupid).

Can someone please help me in this? Thank you.

原文:https://stackoverflow.com/questions/17074169

更新时间:2019-12-18 21:37

最满意答案

因此,即使转换为Uint8Array,我仍然没有得到我需要的结果。 在看了dataURLToBlob方法后,我突然意识到我正在将数组对象传递给Blob构造函数,如:

var blob = new Blob(jpegData_ConvertTo_Uint8Array, "image/jpeg");

这是不正确的,显然为了让Blob工作正常,我需要传递一个数组,如:

var blob = new Blob([jpegData_ConvertTo_Uint8Array], "image/jpeg");

这完美地运行并将我的jpeg写入文件,随后调用FileWriter。 写(blob)函数。 谢谢dandavis!

So even after converting to Uint8Array I still wasn't getting the result that I needed. After looking at the dataURLToBlob method it dawned on me that I was passing the array object to the Blob constructor like:

var blob = new Blob(jpegData_ConvertTo_Uint8Array, "image/jpeg");

This isn't correct and apparently in order to get the Blob to work propery I need to pass an array like:

var blob = new Blob([jpegData_ConvertTo_Uint8Array], "image/jpeg");

This worked perfectly and write my jpeg to the file with a subsequent call to the FileWriter. write(blob) function. Thanks dandavis!

相关问答

我实际上在这里找到了答案: https : //developer.mozilla.org/en/docs/Web/API/Blob 。 可以在IndexedDB中保存FileReader.readAsArrayBuffer的结果。 离线时,可以从此类型数组创建一个blob,然后创建要传递给window.open函数的数据URL。 适用于大文件! I've actually found an answer here: https://developer.mozilla.org/en/docs/We

...

而不是使用相对路径使用绝对路径,如D:\\local.txt 。 它会工作 Instead of Using relative path use absolute path like D:\\local.txt. It will work

因此,即使转换为Uint8Array,我仍然没有得到我需要的结果。 在看了dataURLToBlob方法后,我突然意识到我正在将数组对象传递给Blob构造函数,如: var blob = new Blob(jpegData_ConvertTo_Uint8Array, "image/jpeg");

这是不正确的,显然为了让Blob工作正常,我需要传递一个数组,如: var blob = new Blob([jpegData_ConvertTo_Uint8Array], "image/jpeg");

...

有点像内联图像: p><p>%E5%B0%B1%E5%83%8Ftim_yates%E4%B8%8A%E9%9D%A2%E6%8F%90%E5%88%B0%E7%9A%84%E9%82%A3%E6%A0%B7%EF%BC%8C%20flush%E5%9C%A8%E6%88%91%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%E8%A7%A3%E5%86%B3%E4%BA%86%E9%97%AE%E9%A2%98%E3%80%82%20FileWriter%20writer%20=%20new%20FileWriter(

writer.write("Count, Rate\n")

writer.flush()

Just like tim_yates mentiones above, flush solved the problem in my case. FileWriter writer

...

首先想想关闭你的作家。 关闭时,它应首先flush()您的数据(如此处的文档中所述) 您也可以手动flush() 。 First of all think about closing your writer. When closing, it should flush() your data first (as mentioned in the doc here) You can also flush() manually.

fileWriter.write()是一个异步操作。 你基本上在这里创造了一场比赛 没有超时会发生什么(如果写入速度很慢,甚至会超时): receiveFile(0) fileWriter.seek(fileWriter.length /* == 0 */) queue write(0) receiveFile(1) fileWriter.seek(fileWriter.length /* == 0 */) - 是的,仍然为0因为还没有执行实际写入。 queue write(1) ... actu

...

FileWriter扩展了OutputStreamWriter其构造函数抛出UnsupportedEncodingException 。 如果你看一下FileWriter的实现,它会使用FileOutputStream抛出FileNotFoundException 。 因为FileWriter构造函数现在可以抛出这些异常中的任何一个,所以它被声明为抛出IOException ,这是两个异常的公共超类。 (或者,它可能已被声明抛出两个单独的异常。) FileWriter extends Output

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值