html5 input file img,javascript - Loading an image to a <img> from <input file> - Stack Overflow

Andy E is correct that there is no HTML-based way to do this*; but if you are willing to use Flash, you can do it. The following works reliably on systems that have Flash installed. If your app needs to work on iPhone, then of course you'll need a fallback HTML-based solution.

* (Update 4/22/2013: HTML does now support this, in HTML5. See the other answers.)

Flash uploading also has other advantages -- Flash gives you the ability to show a progress bar as the upload of a large file progresses. (I'm pretty sure that's how Gmail does it, by using Flash behind the scenes, although I may be wrong about that.)

Here is a sample Flex 4 app that allows the user to pick a file, and then displays it:

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"

creationComplete="init()">

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

}

]]>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值