Andy E是正确的,因为没有基于HTML的方式可以做到这一点*;但是如果您愿意使用Flash,则可以这样做。以下内容在已安装Flash的系统上可靠地运行。如果您的应用程序需要在iPhone上运行,那么您当然需要基于HTML的后备解决方案。
*(2013年4月22日更新: HTML5现在已支持HTML。请参阅其他答案。)
Flash上传还具有其他优点-Flash使您能够在大型文件的上传过程中显示进度条。(我敢肯定,通过幕后使用Flash,Gmail就是这样做的,尽管我对此可能是错的。)
这是一个示例Flex 4应用程序,该应用程序允许用户选择一个文件,然后显示该文件:
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="init()">
<fx:Declarations>
</fx:Declarations>
<s:Button x="10" y="10" label="Choose file..." click="showFilePicker()" />
<mx:Image id="myImage" x="9" y="44"/>
<fx:Script>
<![CDATA[
private var fr:FileReference = new FileReference();
// Called when the app starts.
private function init():void
{
// Set up event handlers.
fr.addEventListener(Event.SELECT, onSelect);
fr.addEventListener(Event.COMPLETE, onComplete);
}
// Called when the user clicks "Choose file..."
private function showFilePicker():void
{
fr.browse();
}
// Called when fr.browse() dispatches Event.SELECT to indicate
// that the user has picked a file.
private function onSelect(e:Event):void
{
fr.load(); // start reading the file
}
// Called when fr.load() dispatches Event.COMPLETE to indicate
// that the file has finished loading.
private function onComplete(e:Event):void
{
myImage.data = fr.data; // load the file's data into the Image
}
]]>
</fx:Script>
</s:Application>