Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpStatus 0 when writing file from AS3 to PHP using URLLoader
    text
    copied!<p>I'm doing a project in AS3 where the user can design something and order it after. The design is exported to a PDF, PNG and XML. These files I want to save on the server. On MAMP it all works and I get the HTTPStatus 200, note that I run my SWF from MAMP but the php-script is successfully accessed on the <strong>remote server</strong> and the files are written to the Remote server with succes as well. However when i copy the SWF to the remote server, the files do not save and I get a HTTPStatus 0.</p> <p>In the class that I use (see AS3 code below) I use a URLLoader to get to the following one line php script:</p> <pre><code>&lt;?php file_put_contents($_POST['location'] . $_POST['fileName'], base64_decode($_POST['fileData'])); </code></pre> <p>Here the AS3 class:</p> <pre><code>public class ServerConnection extends Sprite { private var _files:Array = new Array(); private var _fileNames:Array; private var _fileLocation:String = ""; private var _scriptLocation:String; private var _fileSavesInProgress:Array = new Array(); public function ServerConnection(scriptLocation:String = null, files:Array = null, fileNames:Array = null) { if (scriptLocation) setScriptLocation(scriptLocation); if (files) setFiles(files); if (fileNames) setFileNames(fileNames); } public function encodeFiles():void { for(var i:uint = 0; i &lt; _files.length; i++) { _files[i] = encodeFile(_files[i]); } } public function encodeFile(byteArray:ByteArray):Base64Encoder { var base64:Base64Encoder = new Base64Encoder(); base64.encodeBytes(byteArray); return base64; } public function saveFiles(location:String = null):void { if (location) setFileLocation(location); for(var i:uint = 0; i &lt; _files.length; i++) { _files[i] = saveFile(_files[i], _fileNames[i]); } } public function saveFile(encodedFile:Base64Encoder, fileName:String = "test"):void { var data:URLVariables = new URLVariables(); data.fileData = encodedFile; data.fileName = fileName; data.location = _fileLocation; this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "@serverConnection: a save file request was made for " + fileName + "\n" + "url: " + _fileLocation + "\n" + " scriptLocation: " + _scriptLocation + "\n" )); var request:URLRequest = new URLRequest(_scriptLocation); request.method = URLRequestMethod.POST; request.data = data; var loader:URLLoader= new URLLoader(); loader.addEventListener(Event.COMPLETE, function(e:Event):void {fileSaved(loader);}); loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatus); loader.addEventListener(IOErrorEvent.IO_ERROR, ioError); _fileSavesInProgress.push(loader); try { loader.load(request); } catch (e:*) { this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "@serverConnection: error:* = " + e + " \n")); } //navigateToURL(request); } public function displayObjectToPNG(displayObject:*, scale:Number = 1):ByteArray { var bmpData:BitmapData=new BitmapData(displayObject.width, displayObject.height, true, 0xFFFFFF); bmpData.draw(displayObject); var byteArray:ByteArray = PNGEncoder.encode(bmpData); return byteArray; } public function xmlToByteArray(xml:XML):ByteArray { var byteArray:ByteArray = new ByteArray(); byteArray.writeUTFBytes(xml); return byteArray; } public function setScriptLocation(url:String):void { _scriptLocation = url; } public function setFileLocation(url:String):void { _fileLocation = url; } public function setFiles(array:Array, encode:Boolean = true):void { for each(var file:* in array) { for each(var type:XML in describeType(file).extendsClass.@type) { if (type == "flash.display::DisplayObject") file = displayObjectToPNG(file); } if (typeof(file) == "xml") { file = xmlToByteArray(file); } _files.push(file); } if (encode) encodeFiles(); } public function setFileNames(array:Array):void { _fileNames = array; } // EVENTS private function httpStatus(e:HTTPStatusEvent):void { this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "@serverConnection: status = " + e.status + " \n")); } private function ioError(e:IOErrorEvent):void { this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "@serverConnection: IOErrorID = " + e.errorID + " Message: "+ e.text + " \n")); } private function fileSaved(loader:URLLoader):void { this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "@serverConnection: file save completed. " + (_fileSavesInProgress.length -1) + " files to go \n")); _fileSavesInProgress.splice(_fileSavesInProgress.indexOf(loader), 1); if (_fileSavesInProgress.length == 0) { filesSaved(); } } private function filesSaved():void { this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "@serverConnection: files saved \n")); this.dispatchEvent (new ClassAttributesLoaded(ClassAttributesLoaded.CLASS_ATTRIBUTES_LOADED)); } } </code></pre> <p>which I implement like this</p> <pre><code> var s:ServerConnection = new ServerConnection( CONSTANT.SAVE_FILE_SCRIPT_LOCATION, [currentTemplate.getXML(), exampleTemplate, pdf.save(Method.LOCAL)], [CONSTANT.PACKAGE_ID + ".xml", CONSTANT.PACKAGE_ID + ".png", CONSTANT.PACKAGE_ID + ".pdf"] ); s.addEventListener(DebugEvent.MESSAGE, writeToDebug); s.addEventListener(ClassAttributesLoaded.CLASS_ATTRIBUTES_LOADED, exit); s.saveFiles(CONSTANT.RELATIVE_FILE_DIRECTORY); </code></pre> <p><strong>When I change loader.load(request); to navigateToURL(request); the code does work. on the local and remote server.</strong> for obvious reasons I can't use navigateToURL in this case. I do think that the fact that navigateToURL does work and loader.load(request) doesn't say something about the problem... but what? </p> <p>I'm kind of stuck on this and would appreciate help. Thanks in advance!</p> <p>PS: for testing I have set the permission of the remote DIR to 777. Furthermore the files are successfully saved from MAMP to Remote anyhow as stated before.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload