62,025
社区成员
发帖
与我相关
我的任务
分享
<input type="file" runat="server" style="height:22px; width:220px;" onchange="PhotoType();" id="fUpLoad" />
<input type="hidden" id="HFurl" runat="server" /> </td>
<td align="left" >
<asp:ImageButton style="cursor:pointer" ID="iUpLoad" runat="server" ImageUrl="上传图片" Enabled="false" onclick="iUpLoad_Click"/>
<img src="../images/previewphoto.gif"(预览图片) style="cursor:pointer" onclick='ShowPhoto()' />
<img alt="" src=" " height="297" id="iShowPhoto" width="297" />要显示的图片
function PhotoType() {
var x = $("<%=fUpLoad.ClientID %>");
if (!x || !x.value) return;
var patn = /\.jpg$|\.jpeg$|\.gif$/i;
if (patn.test(x.value)) {//如果已经选择了并且类型是图片则浏览不可用
$("<%=iUpLoad.ClientID%>").disabled = false;
}
else {
alert('允许上传图片格式:GIF|JPG|JPEG|。');
$("<%=iAddPhoto.ClientID%>").disabled = false;
}
if (x.value == "" & x.value == null) { alert('请选择要上传的图片。'); }
}
function ShowPhoto() {
$("iShowPhoto").src = "";
$("iShowPhoto").src = $("<%=HFurl.ClientID %>").value;
}
/// <summary>
/// 上传图片并显示出来/并保存到隐藏域路径.以待点击预览图片查看图片
protected void iUpLoad_Click(object sender, ImageClickEventArgs e)
{
string test = Server.MapPath("~/CaseImages"); //用来生成文件夹
if (!Directory.Exists(test))
{
Directory.CreateDirectory(test);
}
int filesize = 4096;
if (fUpLoad.PostedFile.FileName != "")
{
if (fUpLoad.PostedFile.ContentLength / 1024 > filesize)
{
RegisterStartupScript("Startup", "<script>alert('单张图片不能超过4M,请重新选择图片。');</script>");
}
else
{
string imgname = fUpLoad.PostedFile.FileName;
string imgType = imgname.Substring(imgname.LastIndexOf(".") + 1);
string quanname = Guid.NewGuid() + "." + imgType;
string imgurl = "~/CaseImages/" + quanname;
fUpLoad.PostedFile.SaveAs(Server.MapPath(imgurl));
this.HFurl.Value = "../../CaseImages" + "/" + quanname;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript">
function PreviewPhotoatwidth(photo_file,img_object)
{
img_object.src=photo_file.value;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<input name='File' size='50' type='file' onchange='javascript:PreviewPhotoatwidth(this,picpreview)'>
</td>
</tr>
<tr>
<td align="left">
<img id="picpreview" alt="预览" runat="server" />
</td>
</tr>
</table>
</form>
</body>
</html>
<INPUT id="FilePhoto" style="WIDTH: 224px; HEIGHT: 22px" type="file" onchange="javascript:PhoTo(this);" <div id="newPreview"> </div>
<script language="javascript">
function PhoTo(imgFile)
{var newPreview=document.getElementById("newPreview");
newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src =imgFile.value;
newPreview.style.width = "133px";
newPreview.style.height = "162px";
}
</script>