Reputation: 3828
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);
}
}
}
Upvotes: 0
Views: 1898
Reputation: 2135
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.
Upvotes: 1
Reputation: 52133
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:
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)
.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);
Upvotes: 1