A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: upload images to a server from flash

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    17

    upload images to a server from flash

    Hi all, I'm developing a drawing application in flash and I want that when a user has finished his drawing, he can upload the file to a server.
    I found a code on the web that does what I need, but when I upload the image to the server, there open another window and I don't want it does that, I want simply upload the file to the server without opening another window.

    This is the AS code:

    Code:
    import com.adobe.images.JPGEncoder;
    
    save_btn.addEventListener(MouseEvent.CLICK, guardarImg);
    
    function guardarImg(e:MouseEvent):void
    {
        var canvasBmp:BitmapData = new BitmapData(360,300);
        canvasBmp.draw(canvas);
        var myEncoder:JPGEncoder = new JPGEncoder(100);
        var byteArray:ByteArray = myEncoder.encode(canvasBmp);
        //hacemos que se libere la memoria después de almacenar el objeto BitmapData.
        canvasBmp.dispose();
    
        var header:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream");
        var myDate:Date = new Date();
        var timeNow:Number = myDate.getTime();
        timeNow.toString();
    
        var saveJPG:URLRequest = new URLRequest("savejpg.php?nombre=images/dibujo_" + myDate + ".jpg");
        saveJPG.requestHeaders.push(header);
        saveJPG.method = URLRequestMethod.POST;
        saveJPG.data = byteArray;
        
        navigateToURL(saveJPG, "_blank");
    }
    and this is the PHP file:

    PHP Code:
    <?php
    if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
    {
        
    // get bytearray
        
    $jpg $GLOBALS["HTTP_RAW_POST_DATA"];

        
    // add headers for download dialog-box
        //header('Content-Type: image/jpeg');
        
    $nombreArchivo $_GET['nombre'];
        
    $manejadorArchivo fopen($nombreArchivo'w') or die("No se pudo escribir archivo");
        
    fwrite($manejadorArchivo,$jpg);
        
    fclose($manejadorArchivo);
    }
    ?>
    What can I do to avoid it from opening the window when I upload the picture? I've tried to delete this line:
    Code:

    Code:
    navigateToURL(saveJPG, "_blank");
    but if I delete it, the image didn't load.

    thanks!

  2. #2
    Moderator enpstudios's Avatar
    Join Date
    Jun 2001
    Location
    Tampa, Fl.
    Posts
    11,282
    Try this tutorial: CLICK HERE

    It will help you understand the upload process a little better.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    17
    Thanks enpstudios, I'll have a look.

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    17
    Ok the example that enpstudios lives here doesn´t exactly what I want. because I don´t want to load an image from a PC to the server. What I want is to load a drawing that a user do in the flash to a server. Finally I get it using sendToURL. The image loads correctly in the server,but I have one more problem, and is that I want a response if the draw loads correctly or if there was an error, and senToURL method haven´t any response. So I'm trying wiht URLLoader with this code:

    Code:
    private function guardarImg(e:MouseEvent):void
            {
                var canvasBmp:BitmapData = new BitmapData(606,516);
                canvasBmp.draw(canvas);
                var myEncoder:JPGEncoder = new JPGEncoder(100);
                var byteArray:ByteArray = myEncoder.encode(canvasBmp);
    
                canvasBmp.dispose();
                var header:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream");
    
                var myDate:Date = new Date  ;
                var timeNow:Number = myDate.getTime();
                timeNow.toString();
               
                var request:URLRequest = new URLRequest("savejpg.php?nombre=images/draw_" + myDate + ".jpg");
                request.data = byteArray;
                request.method = URLRequestMethod.POST;
                var loader:URLLoader = new URLLoader();
                loader.dataFormat = URLLoaderDataFormat.VARIABLES;
                try
                {
                    loader.load(request);
                    showSaveMessage(msg);
                }
                catch (e:Error)
                {
                    showFailMessage(fmsg);
                }
            }
    But now nothing happens, no error is displayed and the drawing doesn't load.

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    17
    Finally I got it.

    Thanks all for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center