HTML + JS enables local preview before uploading images

Source: Internet
Author: User

How to implement local preview before uploading images seems simple but tricky.

I have encountered such a problem during project development before. I had written the code and thought it was a good function,

So I am not particularly concerned about it. I accidentally studied it today. It was not as simple as I thought. Directly Add code

HTML code:

Copy to ClipboardReference: [www.bkjia.com] <table>
<Tr>
<Td>
<Asp: FileUpload ID = "BtnUpload" runat = "server" onchange = "onFilechange ()"/>
</Td>
</Tr>
<Tr>
<Td>


<Div id = "pic" runat = "server" style = "filter: progid: DXImageTransform. Microsoft. AlphaImageLoader (sizingMethod = scale );
Width: 100px; height: 100px; display: none ">
</Div>

<Asp: Button ID = "Button1" runat = "server" Text = "Upload"/>
</Td>
</Tr>
<Tr>
<Td>
</Td>
</Tr>
</Table>

JS Code:

Copy to ClipboardReference: [www.bkjia.com] <script language = "javascript" type = "text/javascript">
Function onFilechange (){
Var url = $ ("# BtnUpload"). attr ("value ");
$ ("# ImagePhoto" ).css ("width", "0 ");
$ ("# ImagePhoto" ).css ("height", "0 ");
$ ("# Pic" ).css ("display", "block ");
Document. getElementById ("pic"). filters. item ("DXImageTransform. Microsoft. AlphaImageLoader"). src = url;
}
</Script>

Note the following code:

Copy to ClipboardReference: [www.bkjia.com] <div id = "pic" runat = "server" style = "filter: progid: DXImageTransform. Microsoft. AlphaImageLoader (sizingMethod = scale );
Width: 100px; height: 100px; display: none ">
</Div>

The div here is used to preview the photo. The default image control is loaded during initialization. When a new photo is selected, the image control is hidden and the image is displayed.

Filter: Progid: DXImageTransform. Microsoft. AlphaImageLoader (sizingMethod = scale) is a built-in library of Microsoft. It is used to preview local images.

There will be a problem here. In the web page, images do not recognize absolute paths, but only network paths. I think this library should convert local paths into network paths. (Personal guess !)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.