Posting images to Tumblr

package com.rga.nike.women.nfr.signupredesign {
import flash.display.Sprite;
import flash.events.*;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;


/**
* Copied from adobe livedocs
*/
public class FileReferenceWrapper extends Sprite {
public static const ERROR : String = "error";
private var uploadURL : URLRequest;
private var file : FileReference;
private var urlVars : URLVariables;



public function FileReferenceWrapper() {
uploadURL = new URLRequest();
uploadURL.url = "http://www.tumblr.com/api/write";
uploadURL.method = URLRequestMethod.POST;
uploadURL.contentType = "multipart/form-data"; // not really supported.

urlVars = new URLVariables();

urlVars.email = 'youremailhere';
urlVars.password = 'nelson';
urlVars.type = 'photo';
urlVars.state = 'submission';
urlVars['private'] = '1';
uploadURL.data = urlVars;
file = new FileReference();
configureListeners(file);
}

public function browse() : void {
file.browse(getTypes());
}

private function configureListeners(dispatcher : IEventDispatcher) : void {
dispatcher.addEventListener(Event.CANCEL, cancelHandler);
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(Event.SELECT, selectHandler);
dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);
}

private function getTypes() : Array {
var allTypes : Array = new Array(getImageTypeFilter());
return allTypes;
}

private function getImageTypeFilter() : FileFilter {
return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
}

private function getTextTypeFilter() : FileFilter {
return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf");
}

private function cancelHandler(event : Event) : void {
trace("cancelHandler: " + event);
dispatchError();
}

private function dispatchError() : void {
}

private function completeHandler(event : Event) : void {
trace("completeHandler: " + event);
dispatchError();
}

private function uploadCompleteDataHandler(event : Event) : void {
trace("uploadCompleteData: " + event);
}

private function httpStatusHandler(event : HTTPStatusEvent) : void {
trace("httpStatusHandler: " + event);
}

private function ioErrorHandler(event : IOErrorEvent) : void {
trace("ioErrorHandler: " + event);
}

private function openHandler(event : Event) : void {
trace("openHandler: " + event);
}

private function progressHandler(event : ProgressEvent) : void {
var file : FileReference = FileReference(event.target);
trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}

private function securityErrorHandler(event : SecurityErrorEvent) : void {
trace("securityErrorHandler: " + event);
}

private function selectHandler(event : Event) : void {
var file : FileReference = FileReference(event.target);
trace("selectHandler: name=" + file.name + " URL=" + uploadURL.url);
file.upload(uploadURL, 'data');
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *