新浪UED

关于FileReference的上传与下载中断的问题

by Hellena / 前端开发,博客 / 2010-05-12
关于FileReference的上传与下载中断的问题
关于文件上传,目前常见的方式有两种,flash上传组件与Ajax上传组件。由于flash player提供了FileReference及FileReferenceList类支持文件上传(也能进行多文件上传),所以实现起来相较容易一些。但是我在实际使用中也遇到过一些麻烦,在此分享一下我的一些收获,了解FR的机制及一些开发中遇到的问题。

1、关于FileReference的机制介绍
这里是原文地址:http://markshu.ca/imm/flash/tutorial/fileReference.html
以下是原文内容,文章比较长我就不一一翻译了。
[分享]关于FileReference的上传与下载中断的问题

Note Due to security reasons you CANNOT get the file path of the file which you are uploading to the server.

Other then the properties that was mentioned above, there are a few methods that are available for the FileReference API:
The above methods explain themselves pretty well, now we can see how these methods comes in play in the FileReference API
The browse() method opens an operating-system dialog box which prompts the user to select any local file for upload. After a successful call to the browse() method, the upload() method or download() method comes in to play. The upload() method will upload one file at a time and the download() method will prompt the user for a location to save the file and initiates downloading from a remote URL.
Note The download() method is not use in this tutorial, because you can always download a newly uploaded file by type in the server upload directory + the newly add file name. 
The FileReference API do not allow you to set the default file location for the dialog box generated by browse() and download() methods. The default location shown in the dialog box is the most recently browsed folder. If such location cannot be determined, the default location will then direct to the user desktop. The FileReference API does not allow you to read from or write to the transferred file. They do not allow the SWF file that initiated the upload or download to access the uploaded or downloaded file or the file’s location on the user’s disk.
Note You must call FileReference.browse() before calling the download() or upload() method.
We now have enough knowledge to implement a File Upload Application, however we may need some progress checking and Error checking mechanisms, as a result I introduce you the events that are available to the FileReference class.
Note Flash Player can upload files of up to 100MB, also a file size may be limited by the web server itself. 
onCancel = function(fileRef:FileReference) {} 
when the user dismisses the file-browsing dialog box. (clicking the cancel button in the dialog box).
onComplete = function(fileRef:FileReference) {} 
Invoked when the upload or download operation has successfully completed.
onHTTPError = function(fileRef:FileReference, httpError:Number) {} 
Invoked when an upload fails because of an HTTP error.
Note: This event applies only to File Upload and not File Download. If a file download fails, an IO Error is reported instead of a HTTP Error.

onIOError = function(fileRef:FileReference) {}    Invoked when an input/output error occurs.

onOpen = function(fileRef:FileReference) {}  Invoked when an upload or download operation starts.

onProgress = function(fileRef:FileReference, bytesLoaded:Number, bytesTotal:Number) {}
Invoked periodically during the file upload or download operation.

onSecurityError = function(fileRef:FileReference, errorString:String) {}

Invoked when an upload or download fails because of a security error.

In most of the cases this error occurs due to the sandbox restriction, which means the calling SWF file may have tried to access a SWF file outside its domain and does not have permission to do so. You can try to remedy this error by using a cross-domain policy.

onSelect = function(fileRef:FileReference) {} 
Invoked when the user selects a file to upload or download from the file-browsing dialog box.
Now we have all the assets that we need, let’s get into implementing an example using FileReference API which will be an application which you can upload files into a web server.
So what do you need to implement this example?

The Flash application

  • A PHP/ASP/JSP page which will catch the file stream and create the file on the server. In our example, I have chosen to use a PHP script
  • A folder on the Web Server with full read and write permission.
[分享]关于FileReference的上传与下载中断的问题
  FIGURE2: FileReference API application flow char
 
This page is called by the upload() method from the Flash file, when a file is ready to upload. As we discussed before, the file will be resident in the browser’s temp directory, the PHP script. first checks to see whether the file is there by using is_uploaded_file() method. Then we define the directory that the file to be uploaded to (the folder name is uploads in our example). Finally we move the file from the browser’s temp directory to the folder uploads.

                                              
The example we are going to implement will be composed of two parts: one Flash file that will access the file system and grab the file and one PHP page that will do the work of uploading the file to the web server. Create a new PHP file, save it as upload.php, and paste the follow script. inside:

<?php if (is_uploaded_file($_FILES['Filedata']['tmp_name'])){
$uploadDirectory="uploads/";
$uploadFile=$uploadDirectory.basename($_FILES['Filedata']['name']);
copy($_FILES['Filedata']['tmp_name'], $uploadFile); } ?>

2、一个bug提醒,FileReference/FileReferenceList局部定义失效的BUG:http://l4cd.net/Blog/article.asp?id=92 

简单的两段代码:
全局定义FileReferenceList,可正常执行_select
var list:FileReferenceList = new FileReferenceList();
list.addEventListener(Event.SELECT,_select) list.browse();
function _select(e:Event):void { trace(e);
//正常执行 }

局部定义FileReferenceList,无法执行_select

_init() function _init():void {
var list:FileReferenceList = new FileReferenceList();
list.addEventListener(Event.SELECT,_select) list.browse();
} function _select(e:Event):void { trace(e);
//没有执行 }

感觉是局部定义的list在执行完browse后就已经被回收了..无论他是不是有侦听(非弱引用)…

下面的测试对全局/局部的FileReference和FileReferenceList分别进行了测试..
可以发现

Flash Player 9下
全局FileReference,全局FileReferenceList正常
局部FileReference,局部FileReferenceList失效

Flash Player 10下
全局FileReference,局部FileReference,全局FileReferenceList正常
局部FileReferenceList失效

结论是: 少用局部FileReference/FileReferenceList

3、我的问题求助原帖:http://bbs.actionscript3.cn/thread-22790-1-1.html
解决方法: 
cancel()方法确实能够终止上传下载过程。 
以下代码均做为类中的代码段出现,_$FR为类全局定义的FileReference变量 闪吧新社区

第一种写法:
 public function cancelLoad():void

   { 

       _$FR.cancel(); 
   } 
第二种写法:
public function cancelLoad():void 

   { 
      realCancel( _$FR);
   }
   private function realCancel(_$f:FileReference):void
   {
      _$f.cancel();
   }
   以上代码段中,第一种写法不能终止文件上传,第二种写法可以执行。
  原因是,要中断文件的上传下载过程,必须更改FR对象的作用域(好像是在帮助文档的download还是upload方法中有说明)。 

4、处理PHP返回数据信息:
      添加DataEvent.UPLOAD_COMPLETE_DATA事件监听,注意一点哦,监听事件的参数类型需要是DataEvent而不是Event哦,因为Event类是没有data属性的,因为取不到服务器返回的数据。这里就有这样一个问题:http://bbs.actionscript3.cn/viewthread.php?tid=23543&page=1#pid206591
   
   声明:由于需要,本人转载自我的另一blog(http://space.flash8.net/space/?592228),文中除引用文章外均为原创内容。

350-001  
1Y0-201  
NSE4  
HP0-S42  
EX200  
70-417  
2V0-621  
200-125  ,
352-001  
70-246  
MB2-707  
VCP550  
LX0-103  
ICGB  
9A0-385  
200-120  
350-029  
JN0-102  
CAP  
000-105  
ITILFND  
70-488  
ICBB  
ICGB  
220-902  
1Z0-060  
ITILFND  
JK0-022  
JN0-102  
JN0-360  
LX0-103  
LX0-104  
M70-101  
MB2-704  
MB2-707  
MB5-705  
MB6-703  
N10-006  
NS0-157  
NSE4  
OG0-091  
OG0-093  
PEGACPBA71V1  
PMP  
PR000041  
SSCP  
SY0-401  
VCP550  

发表评论

电子邮件地址不会被公开。 必填项已用*标注

* 验证图片 刷新验证码

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>