Simple file uploading using Flex only? - apache-flex

/Flex code/
private const UPLOAD_URL:String = "http://myhosting/upload/upload.php";
private var cer:FileFilter = new FileFilter("Archivos Cer", "*.cer");
private var key:FileFilter = new FileFilter("Archivos Key", "*.key");
private var fileref:FileReference = new FileReference();
private var fileref2:FileReference = new FileReference();
protected function button_clickHandler(event:MouseEvent):void
{
var request:URLRequest = new URLRequest();
request.method = URLRequestMethod.GET;
request.url = UPLOAD_URL;
fileref.upload(request);
fileref2.upload(request);
}
Well, that's the procedure I use to get my files up my server. The "upload.php" in my server is:
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
Is there a way to do this in FLEX only?

You mean, without PHP or some other server-side code? No, you can't. You need some code on the server which can accept the uploaded file.

Related

How to convert component to Image in flex?

We know using file reference save method we can save the component as image in flex 3, but i am using flex 3.0 version, i did not get the save method in file reference class. Its available on flex 3.4.0 onwards.Do we have any other option to make the component to image in flex 3.0 compiler using flex builder IDE?
Thanks in advance
If this case allows to use HTML5, it could be possible.
public function capturePage(): void
{
var bmp:BitmapData = new BitmapData(this.width, this.height);
bmp.draw(this, new Matrix());
// convert to JPEG.
var fileData:ByteArray = new JPEGEncoder().encode(bmp);
var base64: Base64Encoder = new Base64Encoder();
base64.encodeBytes(fileData);
ExternalInterface.call("saveAsImage", "data:image/jpeg;base64,"+base64.toString());
}
index.template.html
<title>${title}</title>
<script language="JavaScript" type="text/javascript">
function saveAsImage(value)
{
// open new window. You will be able to save as image.
window.open(value);
// or use HTML5 FileWriter API here.
}
</script>
<script src="AC_OETags.js" language="javascript"></script>
private function doSave():void
{
var bmp:BitmapData = new BitmapData(canvas.width,canvas.height);
bmp.draw(canvas,new Matrix());
var filedate:ByteArray = new JPEGEncoder().encode(bmp);
var uploadURL:URLRequest = new URLRequest();
uploadURL.url = parentApplication.ServerUrl+"...../imageupload.php?fileName="+yourfilename;
uploadURL.contentType = 'application/octet-stream';
uploadURL.method = URLRequestMethod.POST;
uploadURL.data = filedate;
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
urlLoader.load(uploadURL);
}
PHP File : imageupload.php
<?php
$fileName = $_REQUEST['fileName'] . ".jpeg";
$fp = fopen( "../tempfolder/".$fileName, 'wb' );
fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
fclose( $fp );
?>

readUTFBytes from remote file

Is it possible to readUTFBytes from a remote file without prompting the user with the download screen?
Thank you.
Sure, use URLLoader.
private var loader:URLLoader;
private var req:URLRequest;
private function init():void {
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
req = new URLRequest("..."); //url
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(req);
}
private function onComplete(event:Event):void {
ByteArray(loader.data).readUTFBytes(100);
}

Dynamic tree in Flex

i wanna make a dynamic tree using a lazy loading ,each time i open a folder the tree sends a http request to the server, in this script i'm using just static text to test the tree but , i'm getting in the label of the root all the XML text assigned to the dataprovider, then when i open the root folder i got the childs with good labels , and openitem and closeitem events do not fire how could i make them work , any help is welcome
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import mx.collections.XMLListCollection;
import mx.events.ListEvent;
var origXML:XML;
public function initList()
{
//tree.rootVisible = false;
//TODO: Get this XML from a data service
var origXMLString:String = "<node isBranch=\"true\"><node>supernode1</node>" +
"<node>supernode2</node>" +
//"<node label=\"supernode2\" someProp=\"sdsdf \" isBranch=\"true\"/></node>" +
//"<node label=\"supernode3\" someProp=\"sdsdf \" isBranch=\"true\"/></node>" +
"</node>";
origXML = new XML(origXMLString);
tree.dataProvider = origXML;
}
public function open(event:Object)
{
var selectedNode:Object = event.node;
var myXMLList:XMLList = new XMLList(selectedNode);
//TODO: Get this XML from a data service based on the selected node.
var newXMLString:String = "<childnode1 label=\"childnode1\" someProp=\"sdsdf \" isBranch=\"true\" />" +
"<childnode2 label=\"childnode2\" someProp=\"sdsdf \" isBranch=\"false\" />" +
"<childnode3 label=\"childnode3\" someProp=\"sdsdf \" isBranch=\"true\" />" ;
var myNewXMLList:XMLList = new XMLList(newXMLString);
selectedNode.setChildren(myNewXMLList);
/* myText1.text = selectedNode.toXMLString();
myText2.text = myTree.dataProvider.source[0]; */
tree.dataProvider = origXML;
}
public function close(event:Object)
{
var selectedNode:Object = event.node;
var myXMLList:XMLList = new XMLList(selectedNode);
removeAllDecendants(myXMLList);
/* myText1.text = selectedNode.toXMLString();
myText2.text = myTree.dataProvider.source[0]; */
tree.dataProvider = origXML;
}
public function removeAllDecendants(xmlList:XMLList)
{
var myDescendantXMLList:XMLList = xmlList.descendants();
var myDecendentXMLListCollection:XMLListCollection = new XMLListCollection(myDescendantXMLList);
myDecendentXMLListCollection.removeAll();
}
private function send_data():void {
var loader : URLLoader = new URLLoader();
var request : URLRequest = new URLRequest("http://localhost/index.php" );
// pass the post data
request.method = URLRequestMethod.POST;
var variables : URLVariables = new URLVariables();
variables.s = "haha";
request.data = variables;
// add handlers
loader.addEventListener(Event.COMPLETE, on_complete);
loader.load(request);
// userRequest.send();
}
private function on_complete(e : Event):void{
}
]]>
</fx:Script>
<mx:Tree id="tree" x="103" y="49" width="445" height="278" enabled="true"
itemClose="close(event)" itemOpen="open(event)" selectedIndex="1"></mx:Tree>
Here is a basic one http://flexdiary.blogspot.com/2009/01/lazy-loading-tree-example-file-posted.html
Here is a Mate one
http://www.developria.com/2010/05/refactoring-with-mate.html
Here is a Robotlegs one
http://flexdiary.magnoliamultimedia.com/RobotLegsHierarchicalRemoteObject/RobotLegsHierarchicalRemoteObject.html

Remote Image with basic authentication?

I would like to load a an image from an external domain and I have the below so far:
private function get_coverArt(coverArtID:String):void
{
var requestString:String = "/rest/getCoverArt.view?v=1.5.0&c=AirSub&id=" + coverArtID;
var requestURL:String = subServerURL + requestString;
myCoverArtLoader = new URLLoader();
var myRequest:URLRequest = new URLRequest();
var authHeader:URLRequestHeader = new URLRequestHeader();
authHeader.name = 'Authorization';
authHeader.value = 'Basic ' + credentials;
myRequest.requestHeaders.push(authHeader);
myRequest.url = requestURL;
myRequest.method = URLRequestMethod.GET;
myCoverArtLoader.dataFormat = URLLoaderDataFormat.BINARY;
myCoverArtLoader.addEventListener(Event.COMPLETE, set_coverArt);
myCoverArtLoader.load(myRequest);
}
private function set_coverArt(evt:Event) : void {
coverArtImg = new Image();
var loader:Loader = new Loader();
loader.loadBytes(myCoverArtLoader.data);
coverArtImg.source = loader;
}
This does not seem to work - any help?
Thanks!
Try setting the source directly like so:
private function set_coverArt(evt:Event) : void {
coverArtImg = new Image();
coverArtImg.source = myCoverArtLoader.data;
}
Also, check your authentication, here's a question I answered regarding the auth :
Actionscript 3: Reading an RSS feed that requires authentication

Problem uploading Excel-document. What's wrong?

For some reason, not all of Excel-documents can be upload from my computer. In half the cases get the error ".. no!! error:" from a block of try-catch .. What is wrong?
private function importXLS(e:MouseEvent):void {
fr = new FileReference();
var fileFilter:FileFilter = new FileFilter("Excel (.xls)", "*.xls");
fr.addEventListener(Event.SELECT,selectXLS);
fr.browse([fileFilter]);
statusLabel.text = "selecting...";
}
private function selectXLS(e:Event):void {
fr = FileReference(e.target);
fr.addEventListener(Event.COMPLETE, fileIn);
fr.load();
statusLabel.text = "loading...";
}
private function fileIn(e:Event):void {
ba = new ByteArray();
ba = fr.data;
xls = new ExcelFile();
var flag:Boolean = false;
try{
xls.loadFromByteArray(ba);
flag = true;
}catch(error:Error){
Alert.show("no!! error: " + error.getStackTrace());
}
if (flag == true) {
statusLabel.text = "XlS loaded.";
} else {
statusLabel.text = "XlS didn't load.";
}
}
You are reading the entire file into memory. If a user tries to upload too big a file, their browser will crash. Is there a reason you doing this? Are you using the bytes in the client or just passing them to the server. If you are passing them to the server, you want to just not use the fr.load method that you call in selectXLS(). Instead use fr.upload and avoid your whole problem.

Resources