gpt4 book ai didi

ios - 使用air将文件保存到IOS设备

转载 作者:行者123 更新时间:2023-12-01 18:59:18 24 4
gpt4 key购买 nike

我一直在尝试使用urlstream将文件从服务器保存到ios设备上,但它不起作用(在android设备上可以正常工作。我尝试使用(documentsDirectory),但它也不起作用。我使用了许多其他方法,例如(file.download)和其他,但没有用,请提供任何帮助

我正在使用Flash Pro CS6。

脚本样本:

import flash.filesystem.*;
import flash.events.ProgressEvent;

var urlString:String = "http://example.sample.mp3";
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
var fileData:ByteArray = new ByteArray();
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.addEventListener(ProgressEvent.PROGRESS, progressHandler);


urlStream.load(urlReq);

function loaded(event:Event):void {
urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
writeAirFile();

}

function writeAirFile():void {
var file:File = File.applicationStorageDirectory.resolvePath("sample.mp3");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.close();


trace("The file is written.");
}

function progressHandler(event:Event):void {
trace ("progressHandler: " + event);

}

最佳答案

在iOS上测试

_urlString =  "http://example.sample.mp3";
_urlReq = new URLRequest(_urlString);
_urlStream = new URLStream();
_urlStream.addEventListener(flash.events.ProgressEvent.PROGRESS, progressHandler, false, 0, true);
_urlStream.addEventListener(flash.events.Event.COMPLETE, saveFileToDisc, false, 0, true);
_urlStream.addEventListener(flash.events.IOErrorEvent.IO_ERROR, errorHandler, false, 0, true);
_urlStream.load(_urlReq);


private function progressHandler(evt:flash.events.ProgressEvent):void {
trace("progress: " + event.target.progress);
}

private function errorHandler(evt:flash.events.IOErrorEvent):void {
//do something
}

private function saveFileToDisc(event:flash.events.Event):void {
_fileData = new ByteArray();
_urlStream.readBytes(_fileData, 0, _urlStream.bytesAvailable);

_file = File.applicationStorageDirectory.resolvePath("sample.mp3");
_file.preventBackup = true;
_writeFileStream.addEventListener(flash.events.IOErrorEvent.IO_ERROR, filestreamErrorHandler, false, 0, true);
_writeFileStream.addEventListener(flash.events.Event.CLOSE, fileSaved, false, 0, true);
_writeFileStream.openAsync(_file, FileMode.UPDATE);
_writeFileStream.writeBytes(_fileData, 0, _fileData.length);
_writeFileStream.close();
}

private function filestreamErrorHandler(evt:flash.events.IOErrorEvent):void {
//do something
}

private function fileSaved(closeEvent:flash.events.Event):void {
//trace("file saved");
_writeFileStream.removeEventListener(flash.events.IOErrorEvent.IO_ERROR, filestreamErrorHandler);
_writeFileStream.removeEventListener(flash.events.Event.CLOSE, fileSaved);
_urlString = null;
_urlReq = null;
_urlStream = null;
_file = null;
_fileData.length = 0;
_fileData = null;
}

关于ios - 使用air将文件保存到IOS设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23921270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com