Link to home
Start Free TrialLog in
Avatar of RakeshBhandari
RakeshBhandariFlag for India

asked on

Upload Images to server using flex

Dear experts,

I am working on  Flex4
I am trying to do upload images to server

How to upload images  to server in flex4 using action scrip3.0 ?
Avatar of ianor
ianor

Hmmm.  The only way I'm familiar with is using a PHP script on your server. You'll need to learn some PHP, but it's not terrible, and it's very useful knowledge.  Here's a good tutorial:

http://www.sephiroth.it/tutorials/flashPHP/amfphp_bytearray/

Make sure you look at his previous tutorial link for getting the image data out of the BitmapData object.

Good luck!
Avatar of RakeshBhandari

ASKER

Hi,
    Please let me know how upload  files to server using actionscript 3.0  in flex4 ?
Well there are several ways:

1. Transmitting the byte data using AMF (If you are using ColdFusion, BlazeDS or something similar)
2. Using a HTTP Multipart Post doing it the default way.

Depending on the situation 1 or 2 may be the only vailable solution. So could you please explain in a little more detail what you are trying to do?

I attached a custom component I use to upload files using HTTPPost ... I hope it gets you the idea.
package de.cware.cweb.core.dialogControls {

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;

import mx.controls.Alert;
import mx.controls.ProgressBar;
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;

import spark.components.Button;
import spark.components.Label;
import spark.components.supportClasses.SkinnableComponent;

public class FileUploader extends SkinnableComponent {

    [SkinPart]
    public var uploadButton:Button;

    [SkinPart]
    public var progressBar:ProgressBar;

    [SkinPart]
    public var messageLabel:Label;

    private var fileReference:FileReference;
    private var _fileFilters:Array;
    private var _uploadUrl:String;

    public function FileUploader() {
        super();

        addEventListener(FlexEvent.CREATION_COMPLETE, init);
    }

    private function init(event:FlexEvent):void {
        // Initialize the FileReference.
        fileReference = new FileReference();
        fileReference.addEventListener(Event.SELECT, onFileReferenceSelect);
        fileReference.addEventListener(ProgressEvent.PROGRESS, onFileReferenceProgress);
        fileReference.addEventListener(Event.COMPLETE, onFileReferenceComplete);
        fileReference.addEventListener(FaultEvent.FAULT, onError);
        fileReference.addEventListener(IOErrorEvent.DISK_ERROR, onError);
        fileReference.addEventListener(IOErrorEvent.IO_ERROR, onError);
        fileReference.addEventListener(IOErrorEvent.NETWORK_ERROR, onError);
        fileReference.addEventListener(IOErrorEvent.VERIFY_ERROR, onError);
    }

    override protected function partAdded(partName:String, instance:Object):void {
        super.partAdded(partName, instance);

        if(instance == uploadButton) {
            uploadButton.addEventListener(MouseEvent.CLICK, onButtonClick);
        }
    }

    override protected function partRemoved(partName:String, instance:Object):void {
        super.partRemoved(partName, instance);

        if(instance == uploadButton) {
            uploadButton.removeEventListener(MouseEvent.CLICK, onButtonClick);
        }
    }

    public function get uploadUrl():String {
        return _uploadUrl;
    }

    public function set uploadUrl(value:String):void {
        _uploadUrl = value;
    }

    public function get fileFilters():Array {
        return _fileFilters;
    }

    public function set fileFilters(value:Array):void {
        _fileFilters = value;
    }

    private function onButtonClick(event:MouseEvent):void {
        var imagesFilter:FileFilter = new FileFilter("Images", "*.gif;*.jpg;*.png");
        var docFilter:FileFilter = new FileFilter("Archives", "*.ar;*.bzip2;*.cpio;*.gzip;*.tar;*.zip");
        _fileFilters = [imagesFilter, docFilter];
        fileReference.browse(_fileFilters);
    }

    private function onFileReferenceSelect(evt:Event):void {
        try {
            //messageText = "size (bytes): " + numberFormatter.format(fileReference.size);
            fileReference.upload(new URLRequest(uploadUrl + "&fileName=" + fileReference.name));
        } catch (err:Error) {
            messageText = "ERROR: zero-byte file";
        }
    }

    private function onFileReferenceProgress(evt:ProgressEvent):void {
        trace(evt.bytesTotal + " bytes of " +  evt.bytesLoaded + " uploaded.");
        
        if(progressBar) {
            progressBar.visible = true;
            progressBar.setProgress(evt.bytesTotal / evt.bytesLoaded, 100);
        }
    }

    private function onFileReferenceComplete(evt:Event):void {
        messageText = messageText + " (complete)";
        progressBar.visible = false;
    }

    private function onError(event:Event):void {
        Alert.show(event.toString());
    }

    private function set messageText(msg:String):void {
        if(messageLabel) {
            messageLabel.text = msg;
        }
    }

    private function get messageText():String {
        if(messageLabel) {
            return messageLabel.text;
        }
        return "";
    }
}
}

Open in new window

Hi ChristoferDutz,
I  beginer in  flex . I try to develop Flex air application.  I need to upload  files (if possibile images) from application  to server using actionscript3. I not have any idea about  ColdFusion, BlazeDS
my requirement is when i click browse button user able to select file and click on upload button upload to server usig actionscript 3
ASKER CERTIFIED SOLUTION
Avatar of ChristoferDutz
ChristoferDutz
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
good