我成功地应用了这段代码,它获取一个文本文件并将其放入动态文本框中。这是代码:
// reference code from permadi
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.MovieClip;
import fl.controls.ProgressBarMode;
import fl.controls.TextArea;
var mFileReference:FileReference;
stop();
// Setup button to handle browsing
browseButton.buttonMode = true;
browseButton.mouseChildren = false;
browseButton.addEventListener(MouseEvent.CLICK, onBrowseButtonClicked);
// Hide progress bar;
progressBar.visible = false;
// This function is called when the BROWSE button is clicked.
function onBrowseButtonClicked(event:MouseEvent):void
{
trace("onBrowse");
mFileReference=new FileReference();
mFileReference.addEventListener(Event.SELECT, onFileSelected);
var swfTypeFilter:FileFilter = new FileFilter("Text Files","*.txt; *.html;*.htm;*.php");
var allTypeFilter:FileFilter = new FileFilter("All Files (*.*)","*.*");
mFileReference.browse([swfTypeFilter, allTypeFilter]);
}
// This function is called after user selected a file in the file browser dialog.;
function onFileSelected(event:Event):void
{
trace("onFileSelected");
// This callback will be called when the file is uploaded and ready to use
mFileReference.addEventListener(Event.COMPLETE, onFileLoaded);
// This callback will be called if there's error during uploading;
mFileReference.addEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
// Optional callback to track progress of uploading;
mFileReference.addEventListener(ProgressEvent.PROGRESS, onProgress);
// Tells the FileReference to load the file;
mFileReference.load();
// Show progress bar;
progressBar.visible = true;
progressBar.mode = ProgressBarMode.MANUAL;
progressBar.minimum = 0;
progressBar.maximum = 100;
browseButton.visible = false;
}
// This function is called to notify us of the uploading progress
function onProgress(event:ProgressEvent):void
{
var percentLoaded:Number = event.bytesLoaded / event.bytesTotal * 100;
trace("loaded: "+percentLoaded+"%");
progressBar.setProgress(percentLoaded, 100);
}
// This function is called after the file has been uploaded.;
function onFileLoaded(event:Event):void
{
var fileReference:FileReference = event.target as FileReference;
// These steps below are to pass the data as DisplayObject
// These steps below are specific to this example.
var data:ByteArray = fileReference["data"];
codeText.text = data.toString();
browseButton.visible = true;
progressBar.visible = false;
mFileReference.removeEventListener(Event.COMPLETE, onFileLoaded);
mFileReference.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
mFileReference.removeEventListener(ProgressEvent.PROGRESS, onProgress);
}
function onFileLoadError(event:Event):void
{
// Hide progress bar
progressBar.visible = false;
browseButton.visible = true;
mFileReference.removeEventListener(Event.COMPLETE, onFileLoaded);
mFileReference.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
mFileReference.removeEventListener(ProgressEvent.PROGRESS, onProgress);
trace("File load error");
}
它工作正常,我将加载的文件添加到一个字符串中,但现在我希望Flash读取该字符串并将其发送到与其对应的相关框架。这是代码:
stage.addEventListener(Event.ENTER_FRAME, updateFunction);
function updateFunction(e:Event)
{
if (codeText.text == "Preference 01")
{
gotoAndStop(50);
}
else if (codeText.text == "Preference 02")
{
gotoAndStop(51);
}
else if (codeText.text == "Preference 03")
{
gotoAndStop(52);
}
else if (codeText.text == "Preference 04")
{
gotoAndStop(53);
}
else
{
gotoAndStop(54);
}
}
我不断收到关于空对象的错误 1009,我尝试了几个小时,但我认为我需要一些帮助。
如果您只想根据加载的内容转到不同的框架,只需删除
updateFunction
并将 onFileLoaded()
函数替换为以下内容:
function onFileLoaded(event:Event):void {
var fileReference:FileReference = event.target as FileReference;
// These steps below are to pass the data as DisplayObject
// These steps below are specific to this example.
var data:ByteArray = fileReference["data"];
browseButton.visible = true;
progressBar.visible = false;
mFileReference.removeEventListener(Event.COMPLETE, onFileLoaded);
mFileReference.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
mFileReference.removeEventListener(ProgressEvent.PROGRESS, onProgress);
codeText.text = data.toString();
switch (codeText.text) {
case "Preference 01":
gotoAndStop(50);
break;
case "Preference 02":
gotoAndStop(51);
break;
case "Preference 03":
gotoAndStop(52);
break;
case "Preference 04":
gotoAndStop(53);
break;
default:
gotoAndStop(54);
}
}
请务必启用调试模式并在该模式下进行编译,以便您可以获得错误的行号等详细信息。