Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

changes made to FBAPI.as, /net/FBPost.as, and /net/JSONLoader.as to s…

…upport other types of photo upload
  • Loading branch information...
commit 4d04c74d1fdfe468e4bb027fe5528c36c4b7e369 1 parent 251ce5a
Chris Bowden authored committed
Showing with 90 additions and 1 deletion.
  1. +57 −0 FBAPI.as
  2. +29 −0 net/FBPost.as
  3. +4 −1 net/JSONLoader.as
View
57 FBAPI.as
@@ -113,6 +113,63 @@ package fb {
return loader;
}
+ //Use the following 2 photo upload methods if your File reference
+ //did not come from a native File Browser (eg using File.Browser)
+ public static function uploadPhotoAsFileStream(fileName:String, photo:FileStream, callArgs:Object = null):JSONLoader {
+ if (!FBConnect.api_key || !FBConnect.session)
+ return null;
+
+ var data:ByteArray = new ByteArray();
+ photo.readBytes(data);
+
+ var urlArgs:URLVariables = flattenArgs("photos.upload", callArgs);
+ var post:FBPost= new FBPost();
+
+ for (var urlArg:String in urlArgs)
+ post.writePostData(urlArg, urlArgs[urlArg]);
+
+ post.writeFileDataAsByteArray(fileName, data);
+ post.close();
+
+ var loader:JSONLoader = new JSONLoader();
+
+ var request:URLRequest = new URLRequest(restURL);
+ request.method = URLRequestMethod.POST;
+ request.contentType = "multipart/form-data; boundary="+FBPost.boundary;
+ request.data = post.data;
+ loader.dataFormat = URLLoaderDataFormat.BINARY;
+ loader.load(request);
+
+ return loader;
+ }
+
+ public static function uploadPhotoByteArray(fileName:String, data:ByteArray,
+ callArgs:Object = null):JSONLoader {
+
+ if (!FBConnect.api_key || !FBConnect.session)
+ return null;
+
+ var urlArgs:URLVariables = flattenArgs("photos.upload", callArgs);
+ var post:FBPost= new FBPost();
+
+ for (var urlArg:String in urlArgs)
+ post.writePostData(urlArg, urlArgs[urlArg]);
+
+ post.writeFileDataAsByteArray(fileName, data);
+ post.close();
+
+ var loader:JSONLoader = new JSONLoader();
+
+ var request:URLRequest = new URLRequest(restURL);
+ request.method = URLRequestMethod.POST;
+ request.contentType = "multipart/form-data; boundary="+FBPost.boundary;
+ request.data = post.data;
+ loader.dataFormat = URLLoaderDataFormat.BINARY;
+ loader.load(request);
+
+ return loader;
+ }
+
// Utility function splits out fql.multiquery to it's parts
public static function multiqueryByKey(data:Object):Object {
var resultsByKey:Object = new Object();
View
29 net/FBPost.as
@@ -76,6 +76,35 @@ package fb.net {
writeLineBreak();
}
+ public function writeFileDataAsByteArray(fileName:String, ba:ByteArray):void {
+ var bytes:String;
+
+ writeBoundary();
+ writeLineBreak();
+
+ bytes = 'Content-Disposition: form-data; filename="';
+ for (var i:Number=0; i < bytes.length; i++) {
+ postData.writeByte(bytes.charCodeAt(i));
+ }
+ postData.writeUTFBytes(fileName);
+
+ writeQuotationMark();
+ writeLineBreak();
+
+ bytes = 'Content-Type: image/jpg';
+ for (i=0; i < bytes.length; i++) {
+ postData.writeByte(bytes.charCodeAt(i));
+ }
+
+ writeLineBreak();
+ writeLineBreak();
+
+ //file.data.position = 0;
+ postData.writeBytes(ba);
+
+ writeLineBreak();
+ }
+
public function get data():ByteArray {
postData.position = 0;
return postData;
View
5 net/JSONLoader.as
@@ -82,8 +82,11 @@ package fb.net {
super.load(new_request);
}
+ //call toString on data to ensure the data format is "stringized" before
+ //we try to index it. (uploading photos with a ByteArray caused this) to
+ //crash so I thought I'd fix it in case anything else returns binary, etc.
private function success(event:Event):void {
- if (event.target.data.indexOf("<") != 0) {
+ if (event.target.data.toString().indexOf("<") != 0) {
var eventData:* = JSON.decode(event.target.data);
if (eventData.constructor == Object && eventData.error_code) {
Output.error("Server Error", eventData);
Please sign in to comment.
Something went wrong with that request. Please try again.