[英]Get the local file path through actionscript (or Javascript)
I have a scenario where the user clicks on a menu item in a flex application, that menu item is supposed to give them the file browser on their OS (Mac OSX for now) and then once they choose the file, I pass that file's local path along to another part of the application (that's already written) which makes a connection using that. 我有一种情况,用户单击flex应用程序中的菜单项,该菜单项应该为他们提供其OS(目前为Mac OSX)上的文件浏览器,然后一旦他们选择了文件,我就将该文件传递给本地到应用程序另一部分(已编写)的路径,该部分使用该路径进行连接。
I've done some research and found code that can only be used if I include the AIR SDK which I prefer not to, is there a way to do this in actionscript or javascript (since I can call javascript from my actionscript application)? 我进行了一些研究,发现只有在包含我不喜欢的AIR SDK时才可以使用的代码,有没有办法在ActionScript或javascript中做到这一点(因为我可以从我的actionscript应用程序中调用javascript)?
Edit: the service I am passing the path to requires having the full local path which means that after browsing files, I have to get the local path to pass it along! 编辑:我要传递路径的服务需要具有完整的本地路径,这意味着浏览文件后,我必须获取本地路径才能传递它!
Without using AIR, you can still spawn a file browse dialog for uploading local files using the FileReference class with a Flex or pure ActionScript project. 在不使用AIR的情况下,您仍可以使用FileReference类和Flex或纯ActionScript项目生成文件浏览对话框,以上传本地文件。
This can be abstracted to a service class. 可以将其抽象为服务类。
Application 应用
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
protected function buttonClickHandler(event:MouseEvent):void
{
var uploadService:UploadService = new UploadService("http://destination.com");
uploadService.browse();
}
]]>
</fx:Script>
<s:Button label="Upload..."
click="buttonClickHandler(event)" />
</s:Application>
Upload Service 上载服务
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
public class UploadService extends EventDispatcher
{
public var fileReference:FileReference;
public var fileTypes:FileFilter;
public var uri:String
public function UploadService(uri:String)
{
this.uri = uri;
}
public function browse():void
{
fileTypes = new FileFilter("* (*.*)", "*.*;");
var allTypes:Array = new Array(fileTypes);
fileReference = new FileReference();
fileReference.addEventListener(Event.SELECT, fileSelectHandler);
fileReference.addEventListener(Event.OPEN, fileOpenHandler);
fileReference.addEventListener(Event.COMPLETE, fileCompleteHandler);
fileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, fileSecurityErrorHandler);
fileReference.addEventListener(IOErrorEvent.IO_ERROR, fileIoErrorHandler);
fileReference.browse(allTypes);
}
protected function fileSelectHandler(event:Event):void
{
fileReference.upload(new URLRequest(uri));
}
protected function fileOpenHandler(event:Event):void
{
dispatchEvent(new Event(Event.OPEN));
}
protected function fileCompleteHandler(event:Event):void
{
fileReference.removeEventListener(Event.SELECT, fileSelectHandler);
fileReference.removeEventListener(Event.OPEN, fileOpenHandler);
fileReference.removeEventListener(Event.COMPLETE, fileCompleteHandler);
fileReference.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, fileSecurityErrorHandler);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, fileIoErrorHandler);
dispatchEvent(new Event(Event.COMPLETE));
}
protected function fileSecurityErrorHandler(event:SecurityErrorEvent):void
{
dispatchEvent(new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR));
}
protected function fileIoErrorHandler(event:IOErrorEvent):void
{
dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.