"错误#2038" :在flex中上传文件

时间:2014-01-07 14:49:41

标签: actionscript-3 flex file-upload

在flex中文件上传时,我发现错误为错误#2038 IOEvent。

我google了但我没有找到任何正确的解决方案可能是如何实现的。代码之前正常工作。但现在它显示了这个错误。

这适用于IE,但在其他浏览器中显示错误。

任何有想法的人?

好的,这是AS3代码......请查看

package com.firstplanet.views.actions
{
import com.adobe.serialization.json.JSON;
import com.firstplanet.events.ErrorLogEvent;
import com.firstplanet.events.SaveAdminItemEvent;
import com.firstplanet.model.ModelLocator;
import com.firstplanet.views.modules.admin.winProgress;

import flash.display.DisplayObject;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.KeyboardEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.Responder;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.ui.Keyboard;

import mx.controls.Alert;
import mx.core.FlexGlobals;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;

public class FileUploader
{
    public function FileUploader()
    {
    }

    [Bindable]private var theModel:ModelLocator = ModelLocator.getInstance(); 
    private var urlRequest:URLRequest;
    private var fileReference:FileReference;
    private var fileReferenceList:FileReferenceList;
    private var fileList:Array;
    private var isLargerFile:Boolean;
    private var _winProgress:winProgress;
    private var __errorEvt:ErrorLogEvent;

    [Bindable] public var photoName:String;
    [Bindable] public var theExtension:String;
    private var thePhotoTypeID:Number;
    [Bindable]public var file_name:String="";
    public var theAdminItem:Object = new Object();
    public var serverSideScript:String = "assets/php/FileUploader.php";
    private var max_file_size:Number;
    public var saveDet:Object;
    public var theAdminObj:Object;
    public var isUpload:Boolean;


    public function chooseFiles(fileType:String,fileFormat:String,max_size:Number):void
    {
        fileReferenceList = new FileReferenceList();
        fileReferenceList.addEventListener(Event.SELECT, onSelectFile);
        fileReferenceList.addEventListener(Event.CANCEL,onCancel);
        var arr:Array = [];
        arr.push(new FileFilter(fileType, fileFormat));
        fileReferenceList.browse(arr);
        max_file_size = max_size;
        if(fileType == "All")
        {
            isUpload = true;
        }
    }
    private function onCancel(event:Event):void{
        theModel.tempFlag = false;
    }
    private function convertBytestoMB(maxFileSize:Number):Number
    {
        var theMb:Number;
        theMb = maxFileSize/1024;
        theMb = theMb/1024;

        return theMb;
    }
    public function onSelectFile(event:Event):void
    {
        var fileReferenceList:FileReferenceList = FileReferenceList(event.target);
        fileList = fileReferenceList.fileList;

        // get the first file that the user chose
        fileReference = FileReference(fileList[0]);
        file_name = fileList[0].name;
        //newFileLabel.label=fileList[0].name;

        //if(fileList[0].size > maxFileSize)
        if(fileList[0].size > Number(max_file_size))
        {
            //var theMB:Number = convertBytestoMB(maxFileSize);
            var theMB:Number = convertBytestoMB(max_file_size);
            theModel.uploadStatusMessage = "Selected file is around "+Math.round(convertBytestoMB(fileList[0].size))+" MB. Please select a file that must not exceed "+theMB+ " MB. ";
            theModel.saveErrorLogs("CertificateUpload",theModel.uploadStatusMessage);
            Alert.show(theModel.uploadStatusMessage);
            isLargerFile = true;
            return;             
        }
        else
        {

            theModel.uploadStatusMessage = "";
            isLargerFile = false;
            theModel.saveErrorLogs("CertificateUpload","This is not a large file");
            //saveBtn.enabled = true;

        }

        //previewfileName=fileList[0].name;
        if(isUpload)
        {
            if(theModel.fileManagerVO!=null)
            {
                uploadFile("../../"+theModel.fileManagerVO.folderPath,null,null);
                theModel.saveErrorLogs("CertificateUpload","Upload file path : ../../"+theModel.fileManagerVO.folderPath);
            }
        }
    }

    public function uploadFile(path:String,theAdminItem:Object,saveDetObj:Object):void 
    {
        saveDet = saveDetObj;
        theAdminObj = theAdminItem;

        if(fileList!=null)
        {
            if(isLargerFile)
            {
                Alert.show(theModel.uploadStatusMessage);
                return;
            }
            theModel.deleteOldFile = false;
            if(theAdminItem!=null)
            {
                theModel.deleteOldFile = true;
                if(theAdminItem.url!=null)
                {
                    theModel.oldFileName = theAdminItem.url; 
                }
            }

            _winProgress = winProgress(PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, winProgress, true));
            _winProgress.btnCancel.removeEventListener("click", onUploadCanceled);
            _winProgress.btnCancel.addEventListener("click", onUploadCanceled);
            //_winProgress.title = "Uploading file to " + domain;

            if(fileList!=null)
            {
                _winProgress.txtFile.text = fileList[0].name;
                file_name = fileList[0].name;
            }
            _winProgress.progBar.label = "0%";
            PopUpManager.centerPopUp(_winProgress);

            // Variables to send along with upload
            var sendVars:URLVariables = new URLVariables();
            sendVars.action = "upload";
            sendVars.path = path;

            var request:URLRequest = new URLRequest();
            request.data = sendVars;
            request.url = serverSideScript;
            request.method = URLRequestMethod.POST;
            fileReference = new FileReference();
            fileReference = FileReference(fileList[0]);
            fileReference.addEventListener(Event.OPEN, onUploadOpen);
            fileReference.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
            fileReference.addEventListener(Event.COMPLETE, onUploadComplete);
            fileReference.addEventListener(IOErrorEvent.IO_ERROR, onUploadIoError);
            fileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadSecurityError);
            fileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUploadCompleteReturn);
            fileReference.upload(request);
        }
        else
        {
            //saveItem();
            theModel.deleteOldFile = false;
            submitData(saveDet,theAdminObj);

        }
    }
    private function onUploadOpen(evt:Event):void
    {
        _winProgress.uploadProgressMessage.text = "Uploading please wait..."
    }
    // Get upload progress
    private function onUploadProgress(event:ProgressEvent):void 
    {
        var numPerc:Number = Math.round((Number(event.bytesLoaded) / Number(event.bytesTotal)) * 100);
        _winProgress.progBar.setProgress(numPerc, 100);
        _winProgress.progBar.label = numPerc + "%";
        _winProgress.progBar.validateNow();
        if (numPerc > 90) 
        {
            _winProgress.btnCancel.enabled = false;
            //_winProgress.uploadProgressMessage.text = "Cropping your photo..Please wait..."
        } 
        else 
        {
            _winProgress.uploadProgressMessage.text = "Uploading please wait..."
            _winProgress.btnCancel.enabled = true;
        }
    }
    private function onUploadComplete(event:Event):void 
    {
        PopUpManager.removePopUp(_winProgress);
        _winProgress.uploadProgressMessage.text = "Please wait..."
        theModel.uploadStatusMessage="File have been uploaded.";
        theModel.saveErrorLogs("CertificateUpload",theModel.uploadStatusMessage);
        if(isUpload)
        {
            theModel.fileManagerVO.gatewayConnection.call( "data.files.getFiles", new Responder(theModel.fileManagerVO.onFileResult, theModel.fileManagerVO.onFault),theModel.fileManagerVO.folderPath);
        }
    }
    public function onUploadCompleteReturn(evt:DataEvent):void
    {
        if(evt!=null)
        {
            if(evt.data!=null)
            {
                try
                {
                    theModel.saveErrorLogs("CertificateUpload","came to onUploadCompleteReturn");
                    var rawData:String = evt.data as String;
                    var manager:Array  = JSON.decode(rawData);
                    if(manager!=null && manager[0]!=null)
                    {
                        var tempObj:Object = manager[0] as Object;
                        var status:Boolean = tempObj.theStatus as Boolean;
                        if(status)
                        {
                            if(tempObj.FileName!=null)
                            {
                                photoName = tempObj.FileName as String;
                            }
                            if(tempObj.theExtension!=null)
                            {
                                theExtension = tempObj.theExtension as String;
                            }
                            theModel.uploadStatusMessage="File have been uploaded.";
                            theModel.isPhotoUploaded = true;
                            if(!isUpload)
                            {
                                submitData(saveDet,theAdminObj);
                            }
                        }
                        else
                        {
                            photoName = "";
                            theModel.uploadStatusMessage="That file was not a recognised type or was unable to be decoded.";
                        } 
                    }
                    PopUpManager.removePopUp(_winProgress);

                }
                catch(e:Error)
                {
                    Alert.show("Error in uploading the file");
                }
            }
            else
            {
                Alert.show("Error in uploading the file");
            }
        }
    }
    private function onUploadIoError(event:IOErrorEvent):void 
    {
        theModel.uploadStatusMessage="IO Error in uploading file.";
        PopUpManager.removePopUp(_winProgress);
        theModel.saveErrorLogs("CertificateUpload",event.text);
        //Alert.show("IO Error in uploading file.", "Error");
    }
    // Called on upload security error
    private function onUploadSecurityError(event:SecurityErrorEvent):void 
    {
        theModel.uploadStatusMessage="Security Error in uploading file.";
        theModel.saveErrorLogs("CertificateUpload",theModel.uploadStatusMessage);
        //Alert.show("Security Error in uploading file.", "Error");
        PopUpManager.removePopUp(_winProgress);
        _winProgress == null;
        fileReference.cancel();
    }
    // Called on upload cancel
    private function onUploadCanceled(event:Event):void 
    {
        PopUpManager.removePopUp(_winProgress);
        theModel.uploadStatusMessage = "You have cancelled the operation";
        _winProgress == null;
        fileReference.cancel();
        //clearUpload();
    }

    private function submitData(theObj:Object,theAdminItem:Object):void
    {
        if(photoName!=null)
        {
            theObj["url"] = photoName;
        }
        else
        {
            theObj["url"] = theAdminItem.url;
        }
        if(theExtension!=null)
        {
            theObj["type"] = theExtension;
        }
        else
        {
            theObj["type"] = theAdminItem.audio_type;
        }

        theModel.updateCourseDetails(theObj,'image');
    }

}

}

错误

 [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2038"]

3 个答案:

答案 0 :(得分:0)

嘿,我终于得到了解决方案。如果任何身体面临这样的问题请遵循一些基本步骤

我遇到的问题是文件是在IE(所有版本)上传,但它在其他浏览器中无效。

然后按照以下步骤操作

  • - 首先尝试调度HTTPStatusEvent

  • - 检查即将发生的错误。如果URL有效,则不会给出错误,否则它将显示错误responseURL = null

  • - 然后检查服务器端脚本路径(您用来上传文件的内容)

  • - 在我的情况下,我已将文件的完整网址作为不同的路径传递(assets /.../ fileuploader.php)

  • - 然后将解决错误

你会在互联网上找到很多解决方案。但大多数案例都要检查filereference.IOerrorevent& filereference.HTTPStatusEvent

请参阅此链接:
http://help.adobe.com//en_US/FlashPlatform/reference/actionscript/3/flashnet/FileReference.html#includeExamplesSummary

答案 1 :(得分:0)

外观: Maximum value of maxRequestLength?

我遇到了同样的问题,我用下面的代码解决了web.config到1Gb文件

<system.web>
<httpRuntime maxRequestLength="2097152" />
</system.web>
<system.webServer>
<security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
     </requestFiltering>
 </security>
</system.webServer>

答案 2 :(得分:0)

我尝试这样做并解决了我的问题

在Flex应用程序初始化之后,调用远程服务器方法以检索服务器会话ID。在Java中,远程方法如下所示:

public String getSessionInfo()
{
return FlexContext.getFlexSession().getId();
}