Common picture upload with preview function method

Source: Internet
Author: User

Common picture upload with preview function method
For a common picture preview implementation, but when the remote access to the page will still be unable to preview the situation, but many people use, should also find a problem. The code is as follows.

<%@ page language= "C #" autoeventwireup= "true" codefile= "photoupload.asp tutorial X.cs" inherits= "Photoupload"%>
<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Local picture Preview code (support IE6, IE7) </title>
<style type= "Text/css Tutorial" >
#newpreview
{
Filter:progid:dximagetransform.microsoft.alphaimageloader (Sizingmethod=scale);
}
</style>
<script type= "text/web Effects" language= "JavaScript" >
<!--
function Previewimg (imgfile)
{
The original preview code, does not support IE7.
var Oldpreview = document.getElementById ("Oldpreview");
oldpreview.innerhtml = "

   //New preview code to support IE6, IE7.
    var newpreview = document.getElementById ("Newpreview");
    Newpreview.filters.item ("Dximagetransform.microsoft.alphaimageloader"). src = Imgfile.value ;
    newpreview.style.width = "80px";
    newpreview.style.height = "60px";
   }
   -->
    </script>

<body>
<p> Note: The following is for the internet situation, if you test locally, such as the input address is: HTTP://127.0.0.1/..., you can see the full preview. </p>
<p> If you are using IE6, you can see the following preview, and if you are using IE7, you will not see the following preview. </p>
<div id= "Oldpreview" ></div>
<p> Whether you are using IE6 or IE7, you can see the following preview. </p>
<div id= "Newpreview" ></div>
<p> Please select a picture to preview: <input type= "file" size= "onchange=" javascript:previewimg (this); "/></p>
</body>

So we have to explore another way, the specific implementation of the direct sending code as follows,


<%@ page language= "C #" autoeventwireup= "true" codefile= "Photo.aspx.cs" inherits= "Photo"%>
<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<style type= "Text/css" >
#preview_wrapper
{
Display:inline-block;
width:300px;
height:300px;
Background-color: #ccc;
}
#preview_fake
{
/* This object user displays the preview picture under IE
Filter:progid:dximagetransform.microsoft.alphaimageloader (Sizingmethod=scale);
}
#preview_size_fake
{
/* This object is only used to get the original size of the picture under IE, no other use.
Filter:progid:dximagetransform.microsoft.alphaimageloader (Sizingmethod=image);
Visibility:hidden;
}
#preview
{
/* This object user displays a preview picture under FF/*
width:300px;
height:300px;
}
</style>
<script type= "Text/javascript" >

function Onuploadimgchange (sender)
{
if (!sender.value.match (/.jpg|). Gif|. Png|. bmp/i))//Ignore case
{
Alert (' picture format is not valid! ');
return false;
}
var Objpreview = document.getElementById (' preview ');
var objpreviewfake = document.getElementById (' Preview_fake ');
var objpreviewsizefake = document.getElementById (' Preview_size_fake ');
if (Sender.files && sender.files[0])
{
Objpreview.style.display = ' block ';
objpreview.style.width = ' auto ';
objpreview.style.height = ' auto ';
Firefox has been unable to get full file path directly via Input[file].value for security issues
OBJPREVIEW.SRC = Sender.files[0].getasdataurl ();
}
else if (objpreviewfake.filters)
{
Ie7,ie8 A strange consequence when setting the local picture address to IMG.SRC
(The same environment can sometimes be displayed and sometimes not displayed), so only filters can be used to resolve
IE7, IE8 cannot get the complete file path directly through Input[file].value due to security issues
Sender.select ();
var imgsrc = Document.selection.createrange (). text;
Objpreviewfake.filters.item (' Dximagetransform.microsoft.alphaimageloader '). src = imgsrc;
Objpreviewsizefake.filters.item (' Dximagetransform.microsoft.alphaimageloader '). src = imgsrc;
Autosizepreview (Objpreviewfake, Objpreviewsizefake.offsetwidth, objpreviewsizefake.offsetheight);
Objpreview.style.display = ' None ';
}
}

function Onpreviewload (sender)
{
Autosizepreview (sender, Sender.offsetwidth, sender.offsetheight);
}

function Autosizepreview (Objpre, OriginalWidth, OriginalHeight)
{
var Zoomparam = Clacimgzoomparam (+, originalwidth, originalheight);
Objpre.style.width = zoomparam.width + ' px ';
Objpre.style.height = zoomparam.height + ' px ';
Objpre.style.margintop = zoomparam.top + ' px ';
Objpre.style.marginleft = zoomparam.left + ' px ';
}

function Clacimgzoomparam (maxwidth, maxheight, width, height)
{
var param = {width:width, height:height, top:0, left:0};

if (Width > maxwidth | | height > maxheight)
{
Ratewidth = Width/maxwidth;
Rateheight = Height/maxheight;

if (Ratewidth > Rateheight)
{
Param.width = MaxWidth;
Param.height = Height/ratewidth;
}
Else
{
Param.width = Width/rateheight;
Param.height = MaxHeight;
}
}

Param.left = (maxwidth-param.width)/2;
Param.top = (maxheight-param.height)/2;

return param;
}
</script>


<body>
<form id= "Form1" runat= "Server" >
<table>
<tr>
<td>
<div id= "Preview_wrapper" >
<div id= "Preview_fake" >

</div>
</div>
<br/>
<input id= "upload_img" type= "file" onchange= "Onuploadimgchange (This)" runat= "Server"/>
<span id= "SWH" ></span>
</td>
</tr>
</table>
</form>
</body>

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.