3 Replies Latest reply on May 6, 2008 3:29 AM by aaronbday

    Is there a way to silently upload files?

    aaronbday
      Ok, so I've taken the plunge. I've started working on an AIR app using Flash CS3. I'm putting together a little app that will keep my online store updated with all the product updates from our wholesaler. This includes parsing a csv file, downloading product images from the wholesaler, updating the database, and then uploading the images to our server.
      The problem is that AIR does not seem to have the functionality to silently upload files! Sure, I can use the file.reference object to fire off the browse method, which then opens the OS file browser window BUT I would really like the whole thing to be hands off. In the past, I've written these types of apps using Visual Studio but I haven't touched VS in two years.
      Has anyone run across a way to silently upload or ftp files in AIR?
        • 1. Re: Is there a way to silently upload files?
          Oliver Goldman Adobe Employee
          Yes, you can do this. You can use the File API to select a file without opening a browse dialog, e.g., via File.nativePath.

          Oliver Goldman | Adobe AIR Engineering

          • 2. Re: Is there a way to silently upload files?
            aaronbday Level 1
            Hey Oliver,
            Thanks for your answer--the fact that it CAN be done has gotten me a lot further on finding a solution. Here's what I have so far. As it runs, I can see the progress and completion of the upload but I'm still not getting it saved. I'm uploading it to a ColdFusion processing page.
            AS3 Code:
            import flash.filesystem.*;
            import flash.net.URLRequest;

            //Silent File Upload (no browse window)
            function imageUpload(imageName,uploadPage):void
            {
            trace('Starting Upload of ' + imageName + '\n');
            var myFile:File = File.applicationDirectory.resolvePath(imageName);

            var request:URLRequest = new URLRequest(uploadPage);
            request.method = URLRequestMethod.POST;

            myFile.addEventListener(ProgressEvent.PROGRESS,progressHandler);
            myFile.addEventListener(Event.COMPLETE,completeHandler);
            myFile.upload(request,"theFile");

            }
            function progressHandler(event:ProgressEvent):void {
            var file:FileReference = FileReference(event.target);
            var pLoaded = Math.ceil(event.bytesLoaded/event.bytesTotal*100);
            trace(pLoaded + '% uploaded');
            }
            function completeHandler(event:Event):void
            {
            trace ('Upload complete.');
            }
            var uploadPage = " http://www.aaronbday.com/upload_file.cfm";
            imageUpload("angelic_cat.jpg",uploadPage);

            Here's the CFM page:
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns=" http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Upload File</title>
            </head>

            <body>
            <cfif isDefined("form.theFile")>
            <!---we only want images--->
            <cffile
            action="upload"
            destination="#ExpandPath("test/")#"
            filefield="theFile"
            nameconflict="overwrite"
            accept="image/jpg,image/jpeg,image/gif,image/pjpeg"
            >
            <cfoutput><img src="test/#cffile.serverFile#" /></cfoutput>
            <cfelse>
            <form method="post" action="upload_file.cfm" enctype="multipart/form-data">
            <input type="file" name="theFile" />
            <input type="submit" name="Submit" />
            </form>
            </cfif>
            </body>
            </html>

            Any ideas? Thanks!
            • 3. Re: Is there a way to silently upload files?
              aaronbday Level 1
              Turns out that the Actionscript works fine. The problem was the ColdFusion code. Since uploaded files from flash are encoded with the mime type of application/application/octet-stream, I changed the cffile code to the following and it worked like a charm.
              <cffile action="upload"
              filefield="filedata"
              destination="#ExpandPath("test/")#"
              nameconflict="makeunique"
              accept="application/octet-stream"
              />