当前位置:编程学习 > C#/ASP.NET >>

asp.net 自定义控件实现无刷新上传图片,立即显示缩略图,保存图片缩略图

答案:

如图:

点击浏览,选择图片之后,右面显示图片

第一步:

创建CtFileUpLoad.ascx

复制代码 代码如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CtFileUpLoad.ascx.cs"
Inherits="WebParts_CtFileUpLoad" %>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<iframe src="/WebParts/FileUpLoad.aspx?<%=ParsValue %>" width="240px" height="22px" frameborder="0" scrolling="no"></iframe>
</td>
<td>
<asp:TextBox runat="server" ID="tbFileName" style="display:none"></asp:TextBox>
<div id="dvImg" runat="server" style="position:absolute; margin-left:50px;">
</div>
</td>
</tr>
</table>
<script language="javascript">
function <%=ClientID %>CallLoaded()
{
<% =OnLoaded %>;
}
</script>

复制代码 代码如下:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
public partial class WebParts_CtFileUpLoad : System.Web.UI.UserControl
{
public bool AutoFileName
{
get
{
object ob = ViewState[ClientID + "AutoFileName"];
if (ob == null)
ob = true;
return (bool)ob;
}
set
{
ViewState[ClientID + "AutoFileName"] = value;
}
}
public string UpLoadPath
{
get
{
object ob = ViewState[ClientID + "UpLoadPath"];
if (ob == null)
ob = "UPLOADFILES";
return ob.ToString();
}
set
{
ViewState[ClientID + "UpLoadPath"] = value;
}
}
public string OnLoaded
{
get
{
object ob = ViewState[ClientID + "OnLoaded"];
if (ob == null)
ob = "";
return ob.ToString();
}
set
{
ViewState[ClientID + "OnLoaded"] = value;
}
}
public string SupportExtension
{
get
{
object ob = ViewState[ClientID + "SupportExtension"];
if (ob == null)
{
ob = "gif|png|jpeg|jpg";
}
return ob.ToString();
}
set
{
ViewState[ClientID + "SupportExtension"] = value.Replace(".", "");
}
}
public bool ShowImg
{
get
{
object ob = ViewState[ClientID + "ShowImg"];
if (ob == null)
ob = true;
return (bool)ob;
}
set
{
if ((bool)value)
dvImg.Style["display"] = "block";
else
dvImg.Style["display"] = "none";
}
}
public string FileName
{
get
{
return tbFileName.Text;
}
}
protected string ParsValue = "";
protected void Page_Load(object sender, EventArgs e)
{
ParsValue = "AutoFileName=" + AutoFileName.ToString() + "&SupportExtension=" + SupportExtension + "&UpLoadPath=" + UpLoadPath
+ "&ID=" + ClientID;
}
}

第二步:
创建FileUpLoad.aspx
复制代码 代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpLoad.aspx.cs" Inherits="WebParts_FileUpLoad" %>
<!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">
</head>
<body style="margin:0px;">
<form id="form1" runat="server">
<div>
<asp:FileUpload runat="server" ID="FileUpload1" onchange="upload(this);" />
<asp:Button runat="server" ID="btUp" style="display:none" OnClick="btUp_Click" />
</div>
</form>
</body>
</html>
<script language="javascript">
function upload(ob)
{
var expStr=/.*(<%=SupportExtension%>)$/i;
if(!expStr.test(ob.value))
{
alert("上传文件类型有误。\n(支持文件类型:<%=SupportExtension%>)");
}
else
{
var btUp=document.getElementById("<%=btUp.ClientID %>");
btUp.click();
}
}
</script>

复制代码 代码如下:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.IO;
public partial class WebParts_FileUpLoad : System.Web.UI.Page
{
protected string SupportExtension = "";
protected string UpLoadPath = "";
protected bool AutoFileName;
protected string ParentID = "";
protected void Page_Load(object sender, EventArgs e)
{
SupportExtension = Request.QueryString["SupportExtension"];
UpLoadPath = Request.QueryString["UpLoadPath"];
AutoFileName = bool.Parse(Request.QueryString["AutoFileName"].ToString());
ParentID = Request.QueryString["ID"].ToString();
}
protected void btUp_Click(object sender, EventArgs e)
{
Boolean fileOK = false;
if (FileUpload1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = SupportExtension.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == "." + allowedExtensions[i])
{
fileOK = true;
}
}
if (fileOK)
{
string path = "";
string name = "";
string sPath = "";
if (AutoFileName)
{
name = DateTime.Now.Ticks.ToString() + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
}
else
{
name = FileUpload1.FileName;
}
sPath = Request.PhysicalApplicationPath + "\\" + UpLoadPath + "\\";
path = sPath + name; //图片地址
string fileName = name; //文件名
string fileName_s = "s_" + name; //缩略图文件名称
try
{
//FileInfo file = new FileInfo(name);
// string fileContentType = FileUpload1.PostedFile.ContentType;
//string name = FileUpload1.PostedFile.FileName;//客户端文件路径
//string fileName_sy = "sy_" + file.Name; //水印图文件名称(文字)
//string fileName_syp = "syp_" + file.Name;//水印图文件名称(图片)
//string webFilePath = Server.MapPath(UpLoadPath + "\\" + fileName);
//string webFilePath_s = Server.MapPath(UpLoadPath + "\\" + fileName_s);
string webFilePath = sPath + fileName;//服务器端文件路径
string webFilePath_s = sPath + fileName_s;//服务器端缩略图路径

if (!File.Exists(webFilePath))
{
if (FileUpload1.PostedFile.ContentLength < 2 * 1024 * 1024) //如果图片小于2M
{
try
{
FileUpload1.SaveAs(path); //使用saveAS方法保存文件
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
if (image.Width > 800 || image.Height > 600)
{
MakeThumbnail(webFilePath, webFilePath_s, 800, 600, "W");//生成缩略图的方法
}
else
{
MakeThumbnail(webFilePath, webFilePath_s, 300, 230, "W");//生成缩略图的方法
}
image.Dispose();
// AddShuiYinWord(webFilePath, webFilePath_sy); //保存水印文字图片
// AddShuiYinPi

上一个:C# 调用存储过程简单完整的实例代码
下一个:asp.net JSONHelper JSON帮助类

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,