AS3 在不使用 FileReference.browse() 的情况下将文件上传到服务器
AS3 upload a file to the server without using FileReference.browse()
更新:我正在开发移动版 Air App,我在 applicationDirectory
中有一个名为“database.db”的文件,它是通过 Flash CC IDE 包含或添加的。如果有人能帮助我解决这个问题,我将悬赏 150,也就是说,帮助我在我的服务器上上传脚本,并让我的 ActionScript 将“database.db”文件上传到它。
我希望我的脚本能实现什么?
我想不时备份这个数据库,所以我希望将它上传到服务器。我该怎么做?
下面的代码不起作用并给出了这个错误。
更新:这是我收到的错误:
TypeError: Error #1034: Type Coercion failed: cannot convert "app:/database.db" to flash.net.FileReference.
我不认为我的问题是完全重复的,因为我需要上传一个文件,而给出的示例没有显示如何将数据转换为上传的数据类型。
我有服务器吗?是的,但截至目前,我还没有处理此上传所需的 PHP 脚本,但我目前正在尝试简单地从 AS3 获取文件以准备上传,这就是我所做的做不到。
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);
}
}
}
您的错误来自此代码:
var file:FileReference = FileReference(dir.url);
首先,请注意您没有使用 new FileReference()
,您只需使用 FileReference()
,即 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。第三,你的 fr
从来没有被分配任何东西,所以如果执行到那里 fr.upload()
就会失败。
解决方法:File
inherits from FileReference
and allows upload
调用不受任何限制。所以只需在您的数据库 File
:
上调用 upload
var file:File = File.applicationDirectory.resolvePath("database.db");
var request:URLRequest = new URLRequest();
request.url = UPLOAD_URL;
file.upload(request);
更新
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.
因为您使用的是 PHP 一个好的起点是上传文件的 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']
:
<?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);
?>
注:
- 您的备份目录需要 PHP 可写 (
0777
),然后 move_uploaded_file()
才能工作。您可以使用 SSH, FTP 或根据 PHP 是 运行 的方式,直接从 PHP 脚本使用 chmod($backup_dir, 0777)
. 执行此操作
- 像这样公开任意 public 文件上传不是一个好主意,您不应该制作备份目录 public。理想情况下,您应该需要某种授权才能上传文件,并对上传的文件进行一些验证 file type.
更新 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?
您可以使用 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);
根据其他人的建议,您的完整代码在 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);
}
}
}
下一步是在服务器上有一个 php 脚本来接受文件数据,这是一个您可以使用的简单脚本
<?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;
?>
请确保在将上传的文件移动到所需位置之前检查上传的文件是否有效。简单地将此文件放在服务器上可能会构成安全威胁,因为它可以接受任何类型的文件。
希望对您有所帮助。
更新:我正在开发移动版 Air App,我在 applicationDirectory
中有一个名为“database.db”的文件,它是通过 Flash CC IDE 包含或添加的。如果有人能帮助我解决这个问题,我将悬赏 150,也就是说,帮助我在我的服务器上上传脚本,并让我的 ActionScript 将“database.db”文件上传到它。
我希望我的脚本能实现什么?
我想不时备份这个数据库,所以我希望将它上传到服务器。我该怎么做?
下面的代码不起作用并给出了这个错误。
更新:这是我收到的错误:
TypeError: Error #1034: Type Coercion failed: cannot convert "app:/database.db" to flash.net.FileReference.
我不认为我的问题是完全重复的,因为我需要上传一个文件,而给出的示例没有显示如何将数据转换为上传的数据类型。
我有服务器吗?是的,但截至目前,我还没有处理此上传所需的 PHP 脚本,但我目前正在尝试简单地从 AS3 获取文件以准备上传,这就是我所做的做不到。
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);
}
}
}
您的错误来自此代码:
var file:FileReference = FileReference(dir.url);
首先,请注意您没有使用 new FileReference()
,您只需使用 FileReference()
,即 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。第三,你的 fr
从来没有被分配任何东西,所以如果执行到那里 fr.upload()
就会失败。
解决方法:File
inherits from FileReference
and allows upload
调用不受任何限制。所以只需在您的数据库 File
:
upload
var file:File = File.applicationDirectory.resolvePath("database.db");
var request:URLRequest = new URLRequest();
request.url = UPLOAD_URL;
file.upload(request);
更新
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.
因为您使用的是 PHP 一个好的起点是上传文件的 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']
:
<?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);
?>
注:
- 您的备份目录需要 PHP 可写 (
0777
),然后move_uploaded_file()
才能工作。您可以使用 SSH, FTP 或根据 PHP 是 运行 的方式,直接从 PHP 脚本使用chmod($backup_dir, 0777)
. 执行此操作
- 像这样公开任意 public 文件上传不是一个好主意,您不应该制作备份目录 public。理想情况下,您应该需要某种授权才能上传文件,并对上传的文件进行一些验证 file type.
更新 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?
您可以使用 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);
根据其他人的建议,您的完整代码在 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);
}
}
}
下一步是在服务器上有一个 php 脚本来接受文件数据,这是一个您可以使用的简单脚本
<?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;
?>
请确保在将上传的文件移动到所需位置之前检查上传的文件是否有效。简单地将此文件放在服务器上可能会构成安全威胁,因为它可以接受任何类型的文件。
希望对您有所帮助。