ActionScript的File.upload不适用于iOS设备的Air SDK

我正在尝试使用ActionScript的File.upload上传Air SDK for iOS环境中的文件,但File.upload无法正常工作。 在File.upload被调用之后,没有关于file upload的处理程序被执行,并且没有任何exception被捕获。 当我检查服务器端的networkingstream量时,发现在执行File.upload之后,甚至没有http请求命中服务器。 代码如下。

<?xml version="1.0" encoding="utf-8"?> <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView"> <fx:Declarations> <!-- Place non-visual elements (eg, services, value objects) here --> </fx:Declarations> <fx:Script> <![CDATA[ private var file:File; private var dir:File; //This method is executed to create a file and upload it when the Upload Button is pressed. protected function OnUploadButtonPressed(event:MouseEvent):void{ trace("upload button clicked"); var urlReq:URLRequest = new URLRequest("http://10.60.99.31/MyPath/fileUploadTest.do"); urlReq.method = URLRequestMethod.POST; var str:String = 'This is test'; var imageBytes:ByteArray = new ByteArray(); for ( var i:int = 0; i < str.length; i++ ) { imageBytes.writeByte( str.charCodeAt(i) ); } trace("size = " + imageBytes.length); try{ dir = File.applicationStorageDirectory //I also tested in several different directories //dir = File.createTempDirectory(); //dir = File.documentsDirectory; var now:Date = new Date(); var filename:String = "IMG" + now.fullYear + now.month + now.day + now.hours + now.minutes + now.seconds + now.milliseconds + ".txt"; file = dir.resolvePath( filename ); var stream:FileStream = new FileStream(); stream.open( file, FileMode.WRITE ); stream.writeBytes( imageBytes ); stream.close(); //Read back the file contents to check whether the file is written successfully. var readStream:FileStream = new FileStream(); readStream.open(file, FileMode.READ); var result:String = readStream.readUTFBytes(readStream.bytesAvailable); trace("read back result = " + result);//The result is shown here as expected. file.addEventListener( Event.COMPLETE, uploadComplete ); file.addEventListener( IOErrorEvent.IO_ERROR, ioError ); file.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityError ); file.addEventListener(ErrorEvent.ERROR, someError); file.addEventListener(ProgressEvent.PROGRESS, onProgress); file.upload( urlReq );//This line does not work. No handler is executed. No http request hit the server side. trace("after file upload test"); } catch( e:Error ) { trace( e ); } } //Complete Handler private function uploadComplete( event:Event ):void { trace( "Upload successful." ); } //IOError handler private function ioError( error:IOErrorEvent ):void { trace( "Upload failed: " + error.text ); } //SecurityError handler private function securityError(error:SecurityErrorEvent):void{ trace( "Security error:" + error.text ); } //Other handler private function someError(error:ErrorEvent):void{ trace("some error" + error.text); } //Progress handler private function onProgress(event:ProgressEvent):void{ trace("progressHandler"); } //This method is executed to invoke the URLLoader.load when the Tricky Button is pressed. protected function OnTrickyButtonPressed(event:MouseEvent):void{ var urlReq:URLRequest = new URLRequest("http://200.60.99.31/");//This points to a non-existed server urlReq.method = URLRequestMethod.POST; urlReq.data = new ByteArray(); var loader:URLLoader = new URLLoader(); try{ loader.load(urlReq);//This line seems very important in iOS7. It decides whether the latter file.upload can work. //But in iOS8, file.upload does not work even if this line is executed. trace("after urlloader load"); }catch(e:Error){ trace(e); } } ]]> </fx:Script> <s:Button x="200" y="200" width="400" height="200" label="Upload" click="OnUploadButtonPressed(event)" /> <s:Button x="200" y="500" width="400" height="200" label="Tricky" click="OnTrickyButtonPressed(event)" /> </s:View> 

当在Air Simulator上执行时,它可以正常工作 ,并且文件成功上传到服务器。 但是当在iOS设备(在我的情况下,iPad)上执行时,正如我早些时候解释的那样,没有执行关于file upload的处理程序,也没有http请求事件触发服务器。 所以我觉得这个问题可能在客户端。

在我尝试解决问题的过程中,我发现在iOS7上遇到了一些棘手的问题。 也就是说,如果在调用File.upload方法之前调用URLLoader.load方法(即使URLLoader.load指向不存在的地址),则File.upload将在iOS7上按预期工作。 更具体地说,当上面的方法OnTrickyButtonPressed在方法OnUploadButtonPressed之前执行时,File.upload将在iOS7上成功。 但是这只发生在iOS7上。 在iOS8上,File.upload总是拒绝工作,不pipe之前是否执行了URLLoader.load。

我认为在我的情况下,问题不是在下面的两个链接中描述的沙盒问题或Firefox会话问题,因为甚至没有任何http请求命中服务器端。 似乎iOS的Air SDK只是由于某种原因未能发送http请求。

使用Firefox的Flex 4 FileReference问题

如何让Flexfile upload在Firefox和Safari浏览器上工作?

为了使我的问题更清楚,我列出了以下的环境:

  • 开发环境:Windows7(64位)/ Mac OS 10.9.4(在两个操作系统平台上testing)
  • IDE:Flash Builder 4.7
  • Air SDK:3.8 / 16.0.0(我更新到最新的Air SDK 16.0.0后,问题依然存在。)
  • 应用程序服务器:Tomcat7 + Spring

最后,我想提一下,使用URLLoader.load上传文件在我的情况下不是一个选项,因为我想在将来上传大文件,这是无法用URLLoader.load处理的。

我一直在为此奋斗。 所以我真的很感激,如果有人有任何想法。 提前致谢。

最后我发现真正的问题不在于我提供的代码,而是关于http代理自动configuration,而这个问题发生在iOS7和iOS8上。 我在下面的链接中详细描述了问题和解决方法。 希望这会帮助你们中的一些人。

https://forums.adobe.com/thread/1716036

Interesting Posts