我正在AS3中编写示例代码,Flash Builder 4.6将文件上传到服务器。以下是代码,
var FILE:File = new File("MyPackage.zip");
var URLrequest:URLRequest = new URLRequest(URL);
var variables:URLVariables = new URLVariables();
variables.login = view.LOGIN;
variables.password = view.PASSWORD;
variables.file_name = FILE_NAME;
URLrequest.method = URLRequestMethod.POST;
URLrequest.data = variables;
FILE.addEventListener(Event.OPEN, OpenHandler);
FILE.addEventListener(ProgressEvent.PROGRESS, UploadProgress);
FILE.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, OnSuccessfulUpload);
FILE.addEventListener(IOErrorEvent.IO_ERROR, OnUnsuccessfulUpload);
FILE.upload(URLrequest);
这是UploadProgress函数,
/**
Function: UploadProgress
Description: The function is called at the upload progress of the file.
@param:event event contains the details of the Progress method.
*/
private function UploadProgress(event:ProgressEvent):void
{
try
{
// view.uploadProgress is the ProgressBar component
view.uploadProgress.setProgress(event.bytesLoaded, event.bytesTotal);
}
catch(e:Error)
{
trace(e.message);
}
}
问题是,有时上面的代码导致我的应用程序崩溃。我试过调试但找不到原因。随后,我想说如果未将ProgressEvent侦听器添加到文件对象中,则崩溃将消失。但我的进度也是如此。
我用Google搜索但总是遇到包含FileReference对象的示例,以侦听Event.Select事件以选择要上传的文件。但我不需要它,因为我知道我必须上传哪个文件。还有其他办法吗?
我自己的自定义进度条也有选项。但是,我没有太多的指导。
请帮忙! 感谢