0

I upload a file using adobe air HTTPService.Send method by passing byte array of file content read using FileReference. When I write the set of bytes from the server side (php) it writes the details of the byte array (buff) to the file instead of contents of the file like "endian=bigEndian&position=0&bytesAvailable=61127&length=61127&objectEncoding=3" . Below I have included the code that how I read the file and send it to server. Is there anyway that i can directly pass the data in the byte array to send method?

File Read

var buff:ByteArray = new ByteArray();
var localFilePath:String = "/Videos/sample.txt";
var uploadedFile:File = new File(localFilePath);
var uploadedFileStream:FileStream = new FileStream();
uploadedFileStream.open(uploadedFile,FileMode.READ);
uploadedFileStream.readBytes(buff);
uploadedFileStream.close();

File upload

var serverPath:String = "http://myurl.com/rests";
var service:mx.rpc.http.mxml.HTTPService=new mx.rpc.http.mxml.HTTPService();
service.url=serverPath;
service.requestTimeout=30;
service.method="POST";
service.contentType="application/x-www-form-urlencoded";
service.addEventListener(ResultEvent.RESULT,onResponceReceive);
service.addEventListener(FaultEvent.FAULT,onResponceFail);

  var userName:String="myusername";
var password:String="mypwd";

var encoder:Base64Encoder = new Base64Encoder();
encoder.insertNewLines=false;
encoder.encode(userName+":"+password);
service.headers={Authorization:"Basic "+encoder.toString()};

try{
    service.send(buff);
}catch(error:Error){
    Alert.show(error.message);
}

1 Answer 1

1

Try something like this. You will need to set up some variables.

private function uploadLogo():void {

logof = File.documentsDirectory.resolvePath ("logo.jpg");

ba = new ByteArray();
stream = new FileStream();

stream.open(logof, FileMode.READ);
stream.readBytes(ba);
stream.close();

urlLoaderSend = new URLLoader();
urlLoaderSend.dataFormat = URLLoaderDataFormat.TEXT;
urlLoaderSend.addEventListener(Event.COMPLETE, uploadImage, false, 0, true);

var urlRqSend:URLRequest = new URLRequest("http://www.domain.com/uploadImage.php");
urlRqSend.contentType = "application/octet-stream";
urlRqSend.method = URLRequestMethod.POST;
urlRqSend.data = ba;
urlLoaderSend.load(urlRqSend);

}


private function uploadImage(ev:Event):void {

trace("serverresponse "+ev.target.data);

try {
    ba = null;
    stream = null;
    urlRqSend = null;       
    urlLoaderSend.removeEventListener(Event.COMPLETE, uploadImage);
    urlLoaderSend = null;
} catch (e:Error) {

}

And the PHP

<?php


if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{


$imagename = "image.jpg";


$fp = fopen( "images/$imagename", 'wb' );
fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
fclose( $fp );
echo "OK";}
else { echo "NoImageSent";}?>
Sign up to request clarification or add additional context in comments.

2 Comments

But when I'm authenticating this code with REST basci authentication it prompts me a dialog box asking me to enter username and password. How can I solve this? I have already set this var encoder64:Base64Encoder = new Base64Encoder(); encoder64.insertNewLines=false;
Unfortunately I can't help you there. Perhaps ask a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.