我正在尝试创建一个多图像上传器(想想Coppermine),用户点击SWF中的按钮,选择一个或多个文件,然后将这些文件上传到目录。
我有一个处理上传文件的PHP脚本,我的Flash文件遍历FileReferenceList
中的文件并将文件发送到此PHP脚本进行上传。 PHP脚本中的目录路径是正确的,权限设置为777。
我的ActionScript 3.0类的内容如下所示:
package com.yourfightsite.imageupload {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.external.ExternalInterface;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.URLRequest;
public class ImageUpload extends MovieClip {
private var uploadURL:URLRequest;
private var fileRef:FileReferenceList;
public function ImageUpload():void {
uploadURL = new URLRequest();
uploadURL.url = stage.loaderInfo.parameters.uploadUrl;
fileRef = new FileReferenceList();
uploadButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);
}
private function buttonClickHandler(e:MouseEvent):void {
var imageFilter:FileFilter = new FileFilter('Images', '*.jpg');
fileRef.browse(new Array(imageFilter));
fileRef.addEventListener(Event.SELECT, fileSelectHandler);
}
private function fileSelectHandler(e:Event):void {
for each(var fileToUpload:FileReference in fileRef.fileList) {
addFileToQueue(fileToUpload.name);
uploadSingleFile(fileToUpload);
}
}
private function uploadSingleFile(file:FileReference):void {
file.upload(uploadURL);
file.addEventListener(Event.COMPLETE, completeHandler);
}
private function ioErrorHandler(e:IOErrorEvent):void {
trace('ioErrorHandler: ' + e);
ExternalInterface.call('console.error', e);
}
private function progressHandler(e:ProgressEvent):void {
var fileRef:FileReference = FileReference(e.target);
var percentUploaded:Number = Math.round((e.bytesLoaded/e.bytesTotal)*100);
updateFileInQueue(e.target.name, percentUploaded);
}
private function completeHandler(e:Event):void {
markFileInQueueAsComplete(e.target.name);
}
private function addFileToQueue(filename:String):void {
ExternalInterface.call('addFileToQueue', filename);
}
private function updateFileInQueue(filename:String, percent:Number):void {
ExternalInterface.call('updateFileInQueue', filename, percent);
}
private function markFileInQueueAsComplete(filename:String):void {
ExternalInterface.call('markFileInQueueAsComplete', filename);
}
}
}
类底部的三个方法在我的HTML页面中激活JavaScript函数,将文件名添加到文件队列小部件,并更新它们旁边的百分比值,最后将它们标记为完成。
但是,当我选择文件时,它们会按预期添加到文件队列小部件中,然后什么都没有。文件不会上传,文件队列小部件中的文件不会更新,也不会在发布我的电影时在控制台(我使用的是Chrome)或Flash中引发错误。
任何人都可以在上面的ActionScript中看到任何错误吗?
答案 0 :(得分:0)
不确定这些是否是错误,但这里有一些想法:
是否可以找出fileRef.browse()
是否使用与上传的file
对象相同的对象?
在开始上传之前添加事件监听器:
private function uploadSingleFile(file:FileReference):void {
file.addEventListener(Event.COMPLETE, completeHandler);
file.upload(uploadURL);
}
this post可能有一些相关性。
答案 1 :(得分:0)
这是上传单张照片的一些动作 - 根据您的需求进行调整。 PHP遵循。
private function onUploadCompleteData ( event : DataEvent ) : void
{
var resultXML : XML = new XML( event.data );
if ( resultXML == null ) return;
var storednameString : String = resultXML..filename[0];
}
private function uploadPicture() : void
{
uploadFileReference = new FileReference();
uploadFileReference.addEventListener( Event.SELECT, onSelectFile, false, 0, true );
uploadFileReference.addEventListener( DataEvent.UPLOAD_COMPLETE_DATA, onUploadCompleteData, false, 0, true );
uploadFileReference.addEventListener( IOErrorEvent.IO_ERROR, onUploadError, false, 0, true );
uploadFileReference.browse();
}
private function onUploadError( event : IOErrorEvent ) : void
{
Alert.show( "Unable to upload picture.", "Photo" );
}
private function onSelectFile( event : Event ) : void
{
try
{
// this is where you pass stuff to PHP - file info, storage info
var urlParams : URLVariables = new URLVariables();
urlParams.some_info = some_info;
urlParams.sessionId = phpsessionId;
uploadURL = new URLRequest();
// this is where you'd put the URL for your server
uploadURL.url = serverpathString + "/php/uploadPhoto.php";
uploadURL.method = URLRequestMethod.POST;
uploadURL.data = urlParams;
uploadFileReference.upload( uploadURL );
}
catch ( error : Error )
{
Alert.show( "Unable to upload file.", "Photo" );
}
}
<mx:Form height="100%">
<mx:Button label="Select" click="uploadPicture()"/>
</mx:Form>
PHP上传文件:
<?php
// there is a bug in the flex upload code where the session id isn't being sent. This is a patch for that.
session_id( $_REQUEST[ 'sessionId' ] );
$some_info = $_REQUEST[ 'some_info' ] );
session_start();
$maxSize = 1024 * 1000; // 1 Mb
if ( $_FILES[ 'Filedata' ][ 'size' ] > $maxSize )
{
echo "<ret/>";
return;
}
$filename_inbound = $_FILES[ 'Filedata' ][ 'tmp_name' ];
$utc = microtime( true );
move_uploaded_file( $filename_inbound, "/data/pictures/$filename" );
// if you change the stored filename - here is where it gets returned to actionscript
// return success / fail the same way
echo "<ret><filename>$filename</filename></ret>";
?>