0

UPDATED: I am developing an Air App for mobile and I have a file called "database.db" in the applicationDirectory that is included or added via the Flash CC IDE. I am offering someone a bounty of 150 if they can help me through this issue, that is, help me with the upload script on my server as well as getting my ActionScript to upload the "database.db" file to it.

What do I hope my script will achieve?

I want to back this database up from time to time so I am hoping to upload it to a server. How would I do this?

The code below does not work and gives this error.

UPDATE: Here is the error I am getting:

TypeError: Error #1034: Type Coercion failed: cannot convert "app:/database.db" to flash.net.FileReference.

I don't believe my question is an exact duplicate as I need to upload a file and the example given does not show how the data is turned into a datatype for upload.

Do I have a server? Yes, but as of now I don't have the PHP script needed to handle this the upload, but I am at the moment trying to simple get the file from AS3 to be ready for an upload and this is what I can't do.

package {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequestMethod;
    import flash.events.Event;

    import flash.net.FileReference;
    import flash.net.FileFilter;
    import flash.utils.*;
    import flash.filesystem.File;

    public class Main extends MovieClip {    

        private const UPLOAD_URL: String = "http://mywebsite.com/upload.php";
        private var fr: FileReference;

        public function Main() 
        {    
            var dir: File = File.applicationDirectory;
            dir = dir.resolvePath("database.db");
            trace(dir.url); // app:/database.db

            var file: FileReference = FileReference(dir.url);    

            var request: URLRequest = new URLRequest();
            request.url = UPLOAD_URL;
            fr.upload(request);
        }
    }    
}
Community
  • 1
  • 1
Papa De Beau
  • 3,744
  • 18
  • 79
  • 137
  • Could you be more specific? "Does not work" is too vague -- what, specifically doesn't work? What do you expect it to do? For that matter, how do you expect to upload anything if you don't have a server to upload to? – Brian Feb 03 '16 at 17:11
  • Possible duplicate of [POST file upload using URLRequest](http://stackoverflow.com/questions/9559948/post-file-upload-using-urlrequest) – Brian Feb 03 '16 at 17:14
  • Hi @Brian I made an update. Thanks – Papa De Beau Feb 03 '16 at 18:39
  • I think you're supposed to send your data (could be a string) to php to save as file on your server. That same php could be modified to update a .db file instead of doing a text file save. I would practice (& google) those steps to get an idea of a working result. – VC.One Feb 04 '16 at 05:53

2 Answers2

1

Your error comes from this code:

var file:FileReference = FileReference(dir.url);

First, note that you don't use new FileReference(), you simply use FileReference() which is a form of casting. Because you are trying to cast the string dir.url to a FileReference, you get an error (a cast from String to FileReference is obviously not possible, which is what the error says). Second, if you meant to write new FileReference(dir.url) it would still not be valid, because FileReference has no constructor arguments. Thirdly, your fr is never assigned anything, so fr.upload() would fail if execution ever got there.

Solution: File inherits from FileReference and allows upload to be called without any restrictions. So just call upload on your database File:

var file:File = File.applicationDirectory.resolvePath("database.db");

var request:URLRequest = new URLRequest();
request.url = UPLOAD_URL;
file.upload(request);

Update

I am offering someone a bounty of 150 if they can help me through this issue, that is, help me with the upload script on my server as well as getting my actionscript to upload the database.db file to it.

Since you are using PHP a good starting point is the example given in the documentation. Boiling it down, you just need to use move_uploaded_file() against the uploaded file data found in $_FILES['Filedata'] (which will consist of $_FILES['Filedata']['tmp_name'] and $_FILES['Filedata']['name'] for the uploaded file):

<?php 
    $tmp_file = $_FILES['Filedata']['tmp_name'];
    $upload_file = $_FILES['Filedata']['name'];
    $backup_file = basename($upload_file, '.db') . '_' . date('Y-m-d_H:i:s') . '.db';
    $backup_dir = './db_backups/';
    move_uploaded_file($tmp_file, $backup_dir . $backup_file);
?>

Note:

  • Your backup directory needs to be writable (0777) by PHP before move_uploaded_file() will work. You can do this using SSH, FTP or, depending on how PHP is running, directly from a PHP script using chmod($backup_dir, 0777).
  • It's not a great idea to have have an arbitrary public file upload exposed like this, and you should not make the backup directory public. Ideally, you should require some kind of authorization to upload the file, and do some validation on the uploaded file type.

Update 2

My file will be taken from the applacationStorageDirectory. How do I move a file from the app directory to the storage directory for testing purposes?

You can use File/copyTo() to copy a file:

var file:File = File.applicationDirectory.resolvePath("database.db");
var copyFile:File = File.applicationStorageDirectory.resolvePath("database_backup.db"); 
file.copyTo(copyFile, true);
Community
  • 1
  • 1
Aaron Beall
  • 49,769
  • 26
  • 85
  • 103
  • @PapaDeBeau I see you added a bounty yesterday (and several bounty hunters have added answers now). Did my answer not solve your problem? – Aaron Beall Apr 06 '16 at 14:39
  • I will get back to you. I remember having an issue but I have not looked at it for a bit. I will give the credit to you as you have answered first and I am sure you know how. I think the issue, from what I remember, was with selecting the database.db file without using an action and just having the as3 do it, upload it behind the scenes etc... but I will get back to you. Thanks Aaron. – Papa De Beau Apr 06 '16 at 21:01
  • @Aaron I'm not a bounty hunter, I didn't even saw this question when it has been posted. I added just the PHP part as you didn't put it in your answer. All of us know that this question didn't even need a bounty, just the OP didn't want to spend some minutes to find an answer ... Don't worry about the bounty, I'll delete my post. – akmozo Apr 06 '16 at 21:19
  • @akmozo Ok, I believe you. :) The PHP part was added later. I just saw all the sudden answers being added after it had been sitting idle for days. Peace. – Aaron Beall Apr 06 '16 at 21:27
  • so if someone saw a post on top because its featured and added answer will be called bounty hunters. And then you edited answer with info provided by others will be a solution solver. I don't believe that. – Sameer Kumar Jain Apr 07 '16 at 13:30
  • Hi @Aaron, it worked. Great script and great ideas. One last question. My file will be taken from the applacationStorageDirectory. How do I move a file from the app directory to the storage directory for testing purposes? – Papa De Beau Apr 07 '16 at 17:55
  • @PapaDeBeau You can use [`File/copyTo()`](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html#copyTo()). I'll add an example for your case. – Aaron Beall Apr 08 '16 at 14:43
1

As suggested by others, your complete code must be looks like this in flash.

package 
{
    import flash.filesystem.File;
    import flash.display.MovieClip;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.net.URLRequestMethod;

    public class Main extends MovieClip
    {
        private const UPLOAD_URL: String = "http://mywebsite.com/upload.php";

        public function Main()
        {
            var file:File = File.applicationDirectory.resolvePath("database.db");

            var ur:URLRequest = new URLRequest();

            //Extra parameters you want to send with the filedata
            var uv:URLVariables     =   new URLVariables();
            uv.filename             =   'file_name_of_your_choice.db';

            //attach data to request
            ur.data                 =   uv;

            //request method
            ur.method               =   URLRequestMethod.POST;

            //request URL
            request.url = UPLOAD_URL;

            //Finally Upload it
            file.upload(request);
        }
    }
}

Next is to have a php script on server to accept filedata, here is a simple script you can use

<?php
    //make sure you have this directory created on server and have enough permissions to write files
    $dir = 'dbbackup';
    $backup_file = $dir . '/' . $_POST['filename'];
    //You should make your checks on the files here if its valid and allowed to be placed


    //allow to write files in directory
    chmod($dir, 0777); 
    $status = ""
    if(move_uploaded_file($_FILES['Filedata']['tmp_name'], $backup_file))
    {
         $status = "success";
    }
    else
    {
        $status = "failed";
    }
    //reset permission back to read only
    chmod($dir, 0644); 
    echo  $status;
?>

Please make sure to check if the uploaded file is valid file before moving it to desired location. Simply placing this file on server could be security threats as it can accept any kind of file.

I hope this helps.

Sameer Kumar Jain
  • 2,074
  • 1
  • 13
  • 22