appcelerator

时间:2017-06-22 08:59:15

标签: file titanium appcelerator iostream

我正在使用以下代码将从API接收的字节数组转换为文件对象:

var buffer = [73,109,112,111,114,116,115,32,83,121,115,116,101,....];//byte array
var outBuffer = Ti.createBuffer({length: size });
        for(var i=0;i<size;i++){
            outBuffer[i] = buffer[i];
        }
var inBufferStream = Ti.Stream.createStream({ source: outBuffer, mode: Ti.Stream.MODE_READ });
var outFileStream = Ti.Filesystem.getFile("test.txt").open(Ti.Filesystem.MODE_WRITE);
var bytesWritten = Ti.Stream.writeStream(inBufferStream, outFileStream, 1024);
outFileStream.close();

创建文件需要太多时间。

有没有更好的方法在appcelerator中执行此操作?请帮忙。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码 -

var data = Ti.Utils.base64decode(fileString);
var blobStream = Ti.Stream.createStream({ source: data, mode:Ti.Stream.MODE_READ });
var newBuffer = Ti.createBuffer({ length: data.length });
var bytes = blobStream.read(newBuffer);
f.write(fileString);

如果您有任何疑问,请告诉我。

谢谢&amp;问候 杰滕德拉