WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="mms.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
<style type=text/css>TD { FONT-SIZE: 9pt }
</style>
<script language=JavaScript>
function addFile()
{
var str = '<INPUT type="file" size="50" NAME="File">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
function writeMMS() {
mymms.document.open();
mymms.document.writeln("<html>");
mymms.document.writeln("<head>");
mymms.document.writeln("<style type=\"text/css\">");
mymms.document.writeln(".bgselfwrite {");
mymms.document.writeln(" border: 1px none #999999;");
mymms.document.writeln(" background-position: center top;");
mymms.document.writeln(" line-height: 20px;");
mymms.document.writeln(" font-size: 12px;");
mymms.document.writeln(" background-repeat: repeat-xy;");
mymms.document.writeln("}");
mymms.document.writeln("</style>");
mymms.document.writeln("</head>");
mymms.document.writeln("<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\" class=\"bgselfwrite\">");
mymms.document.writeln("");
mymms.document.writeln("</body>");
mymms.document.writeln("</html>");
mymms.document.close();
mymms.document.designMode='On';
mymms.focus();
}
function insertImg(src)
{
mymms.focus();
mymms.document.execCommand('InsertImage',false,src);
}
function insertText(text){
mymms.focus();
mymms.document.body.innerHTML = mymms.document.body.innerHTML + text;
}
function getSource(){
alert(mymms.document.body.innerHTML);
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout" onload=";writeMMS();">
<form id="frm" method="post" encType="multipart/form-data" runat="server">
<div align=center>
<h3>彩信精灵(上传多张自己的图片,然后编辑,加上文字)</h3>
<P id=MyFile><INPUT type=file size=50 name=File></P>
<P>类别<asp:dropdownlist id=strClass runat="server" AutoPostBack="True">
<asp:ListItem Value="a">所有图片</asp:ListItem>
<asp:ListItem Value="山水图片">山水图片</asp:ListItem>
<asp:ListItem Value="人物图片">人物图片</asp:ListItem>
<asp:ListItem Value="QQ图片">QQ图片</asp:ListItem>
<asp:ListItem Value="文字图片">文字图片</asp:ListItem>
<asp:ListItem Value="其他类别">其他类别</asp:ListItem>
</asp:dropdownlist><input onclick=addFile() type=button value=增加(Add)>
<input onclick=this.form.reset() type=button value=重置(ReSet)>
<asp:button id=UploadButton Text="开始上传" Runat="server"></asp:button></P>
<P><FONT face=宋体><asp:label id=strStatus runat="server"></asp:label></FONT></P></div>
<TABLE borderColor=red cellSpacing=0 cellPadding=0 width=775 align=center border=1>
<TBODY>
<TR>
<td vAlign=top>
<iframe name=mymms marginWidth=0 marginHeight=0 frameBorder=0 width=200 scrolling=0 height=300></iframe><br>Tel:<input
type=text maxLength=11 size=11 name=mobile>
<input name=bt type=button value="发送" onclick="javascript:getSource();"><br></td>
<TD align="center" valign="top">
<asp:Panel id="Panel1" runat="server" Width="580px"></asp:Panel><FONT face="宋体"></FONT></TD></TR></TBODY></TABLE></form>
</body>
</HTML>
//**************************************************************8
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace mms
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
////1。把图片文件(JPG GIF PNG)上传,
////2。保存到指定的路径(在web.config中设置路径,以文件的原有格式保存),
////3。并自动生成指定宽度的(在web.config中设置宽度)
////4。和指定格式的(在web.config中指定缩略图的格式)
////5。和原图比例相同的缩略图(根据宽度和原图的宽和高计算所略图的高度)
////6。可以判断是否已经存在文件
////7。如果不覆盖,则给出错误
////8。如果选中"覆盖原图"checkbox,则覆盖原图。
////9。可以根据要求,在webform上设置1个以上的file input和相应的checkbox
////10。并在文件上传完毕后,显示原图的文件名,尺寸,字节,和
////11。缩略图的文件名尺寸。
////12。缩略图的文件名格式:原图+"_thumb."+指定格式,如:test.jpg_thumb.gif,以便于管理。
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.DropDownList strClass;
protected System.Web.UI.WebControls.Label strStatus;
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
//this.SaveImages();
ViewPic("a");
}
}
public void ViewPic(string str)
{
//显示图片
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
myConnection.Open();
string sQuery;
if (str=="a")
{
sQuery = "select imgThumbnail,FileName,ContentLength,imgID from T_UpFileInfo order by imgID desc";
}
else
{
sQuery = "select imgThumbnail,FileName,ContentLength,imgID from T_UpFileInfo where strClass='"+str+"' order by imgID desc";
}
SqlCommand sqlCmd = new SqlCommand(sQuery,myConnection);
SqlDataReader dr = sqlCmd.ExecuteReader();
while(dr.Read())
{
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
img.ImageUrl="uppic/"+dr["imgThumbnail"].ToString();
img.BorderWidth=2;
img.BorderColor=Color.White;
img.Attributes.Add("onclick","javascript:insertImg(this.src);");
Panel1.Controls.Add(img);
}
dr.Close();
myConnection.Close();
}
public string MakeFile()
{
//根据时间得到文件名
string fname;
fname=DateTime.Now.ToString();
fname = fname.Replace("-","");
fname = fname.Replace(" ","");
fname = fname.Replace(":","");
fname = fname.Replace("PM","");
fname = fname.Replace("AM","");
fname = fname.Replace("上午","");
fname = fname.Replace("下午","");
return fname;
}
public Boolean SaveImages()
{
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
myConnection.Open();
//'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
System.Drawing.Image oriImg, newImg;
string strFePicSavePath;
strFePicSavePath = System.Web.HttpContext.Current.Request.MapPath("uppic/");
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
//'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension, strFile ,imgNameOnly,imgThumbnail;
strFile=this.MakeFile();
fileName = System.IO.Path.GetFileName(postedFile.FileName);
//if (fileName != "" && postedFile.ContentLength > 0 && postedFile.ContentLength < 51200)
if (fileName != "" && postedFile.ContentLength > 0)
{
fileExtension = System.IO.Path.GetExtension(fileName);
if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".gif" || fileExtension.ToLower() == ".bmp")
{
strFile=strFile+iFile.ToString();
fileName = strFile + fileExtension.ToLower();
imgNameOnly=strFile+"_s";
strMsg.Append("上传的文件大小:" + postedFile.ContentLength.ToString() + "<br>");
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
//'可根据扩展名字的不同保存到不同的文件夹
//注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("uppic/") + fileName);
//生成缩略图
//**************************************************
oriImg = System.Drawing.Image.FromStream(postedFile.InputStream);
newImg = oriImg.GetThumbnailImage(80, 80 * oriImg.Height/oriImg.Width,null,new System.IntPtr(0));
switch(fileExtension)
{
//jpeg format can get the smallest file size, and the png is the largest size
//case "jpeg":
case ".jpg":
newImg.Save(strFePicSavePath + imgNameOnly + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + ".jpg";
break;
case ".gif":
newImg.Save(strFePicSavePath + imgNameOnly + ".gif",System.Drawing.Imaging.ImageFormat.Gif);
imgThumbnail = imgNameOnly + ".gif";
break;
case ".png":
newImg.Save(strFePicSavePath + imgNameOnly + ".png",System.Drawing.Imaging.ImageFormat.Png);
imgThumbnail = imgNameOnly + ".png";
break;
default:
newImg.Save(strFePicSavePath + imgNameOnly + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + ".jpg";
break;
}//switch
//***************************************************
string sQuery = "insert into T_UpFileInfo (ContentType,FileName,FileExtension,ContentLength,UserName,strClass,imgThumbnail)" +
"values('" + postedFile.ContentType.ToString()+ "','" + fileName+ "','" + fileExtension+ "','" + postedFile.ContentLength.ToString()+ "','admin','"+this.strClass.SelectedValue+"','"+imgThumbnail+"')";
SqlCommand sqlCommand = new SqlCommand(sQuery,myConnection);
sqlCommand.ExecuteNonQuery();
newImg.Dispose();
oriImg.Dispose();
}
}
}
//strStatus.Text = strMsg.ToString();
myConnection.Close();
ViewPic(this.strClass.SelectedItem.Value);
return true;
}
catch(System.Exception Ex)
{
myConnection.Close();
ViewPic(this.strClass.SelectedItem.Value);
strStatus.Text = Ex.Message;
return false;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.strClass.SelectedIndexChanged += new System.EventHandler(this.strClass_SelectedIndexChanged);
this.UploadButton.Click += new System.EventHandler(this.UploadButton_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void strClass_SelectedIndexChanged(object sender, System.EventArgs e)
{
string strClass;
strClass=this.strClass.SelectedValue;
strStatus.Text = strClass;
ViewPic(this.strClass.SelectedItem.Value);
}
private void UploadButton_Click(object sender, System.EventArgs e)
{
this.SaveImages();
}
}
}
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="mms.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
<style type=text/css>TD { FONT-SIZE: 9pt }
</style>
<script language=JavaScript>
function addFile()
{
var str = '<INPUT type="file" size="50" NAME="File">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
function writeMMS() {
mymms.document.open();
mymms.document.writeln("<html>");
mymms.document.writeln("<head>");
mymms.document.writeln("<style type=\"text/css\">");
mymms.document.writeln(".bgselfwrite {");
mymms.document.writeln(" border: 1px none #999999;");
mymms.document.writeln(" background-position: center top;");
mymms.document.writeln(" line-height: 20px;");
mymms.document.writeln(" font-size: 12px;");
mymms.document.writeln(" background-repeat: repeat-xy;");
mymms.document.writeln("}");
mymms.document.writeln("</style>");
mymms.document.writeln("</head>");
mymms.document.writeln("<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\" class=\"bgselfwrite\">");
mymms.document.writeln("");
mymms.document.writeln("</body>");
mymms.document.writeln("</html>");
mymms.document.close();
mymms.document.designMode='On';
mymms.focus();
}
function insertImg(src)
{
mymms.focus();
mymms.document.execCommand('InsertImage',false,src);
}
function insertText(text){
mymms.focus();
mymms.document.body.innerHTML = mymms.document.body.innerHTML + text;
}
function getSource(){
alert(mymms.document.body.innerHTML);
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout" onload=";writeMMS();">
<form id="frm" method="post" encType="multipart/form-data" runat="server">
<div align=center>
<h3>彩信精灵(上传多张自己的图片,然后编辑,加上文字)</h3>
<P id=MyFile><INPUT type=file size=50 name=File></P>
<P>类别<asp:dropdownlist id=strClass runat="server" AutoPostBack="True">
<asp:ListItem Value="a">所有图片</asp:ListItem>
<asp:ListItem Value="山水图片">山水图片</asp:ListItem>
<asp:ListItem Value="人物图片">人物图片</asp:ListItem>
<asp:ListItem Value="QQ图片">QQ图片</asp:ListItem>
<asp:ListItem Value="文字图片">文字图片</asp:ListItem>
<asp:ListItem Value="其他类别">其他类别</asp:ListItem>
</asp:dropdownlist><input onclick=addFile() type=button value=增加(Add)>
<input onclick=this.form.reset() type=button value=重置(ReSet)>
<asp:button id=UploadButton Text="开始上传" Runat="server"></asp:button></P>
<P><FONT face=宋体><asp:label id=strStatus runat="server"></asp:label></FONT></P></div>
<TABLE borderColor=red cellSpacing=0 cellPadding=0 width=775 align=center border=1>
<TBODY>
<TR>
<td vAlign=top>
<iframe name=mymms marginWidth=0 marginHeight=0 frameBorder=0 width=200 scrolling=0 height=300></iframe><br>Tel:<input
type=text maxLength=11 size=11 name=mobile>
<input name=bt type=button value="发送" onclick="javascript:getSource();"><br></td>
<TD align="center" valign="top">
<asp:Panel id="Panel1" runat="server" Width="580px"></asp:Panel><FONT face="宋体"></FONT></TD></TR></TBODY></TABLE></form>
</body>
</HTML>
//**************************************************************8
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace mms
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
////1。把图片文件(JPG GIF PNG)上传,
////2。保存到指定的路径(在web.config中设置路径,以文件的原有格式保存),
////3。并自动生成指定宽度的(在web.config中设置宽度)
////4。和指定格式的(在web.config中指定缩略图的格式)
////5。和原图比例相同的缩略图(根据宽度和原图的宽和高计算所略图的高度)
////6。可以判断是否已经存在文件
////7。如果不覆盖,则给出错误
////8。如果选中"覆盖原图"checkbox,则覆盖原图。
////9。可以根据要求,在webform上设置1个以上的file input和相应的checkbox
////10。并在文件上传完毕后,显示原图的文件名,尺寸,字节,和
////11。缩略图的文件名尺寸。
////12。缩略图的文件名格式:原图+"_thumb."+指定格式,如:test.jpg_thumb.gif,以便于管理。
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.DropDownList strClass;
protected System.Web.UI.WebControls.Label strStatus;
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
//this.SaveImages();
ViewPic("a");
}
}
public void ViewPic(string str)
{
//显示图片
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
myConnection.Open();
string sQuery;
if (str=="a")
{
sQuery = "select imgThumbnail,FileName,ContentLength,imgID from T_UpFileInfo order by imgID desc";
}
else
{
sQuery = "select imgThumbnail,FileName,ContentLength,imgID from T_UpFileInfo where strClass='"+str+"' order by imgID desc";
}
SqlCommand sqlCmd = new SqlCommand(sQuery,myConnection);
SqlDataReader dr = sqlCmd.ExecuteReader();
while(dr.Read())
{
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
img.ImageUrl="uppic/"+dr["imgThumbnail"].ToString();
img.BorderWidth=2;
img.BorderColor=Color.White;
img.Attributes.Add("onclick","javascript:insertImg(this.src);");
Panel1.Controls.Add(img);
}
dr.Close();
myConnection.Close();
}
public string MakeFile()
{
//根据时间得到文件名
string fname;
fname=DateTime.Now.ToString();
fname = fname.Replace("-","");
fname = fname.Replace(" ","");
fname = fname.Replace(":","");
fname = fname.Replace("PM","");
fname = fname.Replace("AM","");
fname = fname.Replace("上午","");
fname = fname.Replace("下午","");
return fname;
}
public Boolean SaveImages()
{
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
myConnection.Open();
//'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
System.Drawing.Image oriImg, newImg;
string strFePicSavePath;
strFePicSavePath = System.Web.HttpContext.Current.Request.MapPath("uppic/");
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
//'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension, strFile ,imgNameOnly,imgThumbnail;
strFile=this.MakeFile();
fileName = System.IO.Path.GetFileName(postedFile.FileName);
//if (fileName != "" && postedFile.ContentLength > 0 && postedFile.ContentLength < 51200)
if (fileName != "" && postedFile.ContentLength > 0)
{
fileExtension = System.IO.Path.GetExtension(fileName);
if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".gif" || fileExtension.ToLower() == ".bmp")
{
strFile=strFile+iFile.ToString();
fileName = strFile + fileExtension.ToLower();
imgNameOnly=strFile+"_s";
strMsg.Append("上传的文件大小:" + postedFile.ContentLength.ToString() + "<br>");
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
//'可根据扩展名字的不同保存到不同的文件夹
//注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("uppic/") + fileName);
//生成缩略图
//**************************************************
oriImg = System.Drawing.Image.FromStream(postedFile.InputStream);
newImg = oriImg.GetThumbnailImage(80, 80 * oriImg.Height/oriImg.Width,null,new System.IntPtr(0));
switch(fileExtension)
{
//jpeg format can get the smallest file size, and the png is the largest size
//case "jpeg":
case ".jpg":
newImg.Save(strFePicSavePath + imgNameOnly + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + ".jpg";
break;
case ".gif":
newImg.Save(strFePicSavePath + imgNameOnly + ".gif",System.Drawing.Imaging.ImageFormat.Gif);
imgThumbnail = imgNameOnly + ".gif";
break;
case ".png":
newImg.Save(strFePicSavePath + imgNameOnly + ".png",System.Drawing.Imaging.ImageFormat.Png);
imgThumbnail = imgNameOnly + ".png";
break;
default:
newImg.Save(strFePicSavePath + imgNameOnly + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + ".jpg";
break;
}//switch
//***************************************************
string sQuery = "insert into T_UpFileInfo (ContentType,FileName,FileExtension,ContentLength,UserName,strClass,imgThumbnail)" +
"values('" + postedFile.ContentType.ToString()+ "','" + fileName+ "','" + fileExtension+ "','" + postedFile.ContentLength.ToString()+ "','admin','"+this.strClass.SelectedValue+"','"+imgThumbnail+"')";
SqlCommand sqlCommand = new SqlCommand(sQuery,myConnection);
sqlCommand.ExecuteNonQuery();
newImg.Dispose();
oriImg.Dispose();
}
}
}
//strStatus.Text = strMsg.ToString();
myConnection.Close();
ViewPic(this.strClass.SelectedItem.Value);
return true;
}
catch(System.Exception Ex)
{
myConnection.Close();
ViewPic(this.strClass.SelectedItem.Value);
strStatus.Text = Ex.Message;
return false;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.strClass.SelectedIndexChanged += new System.EventHandler(this.strClass_SelectedIndexChanged);
this.UploadButton.Click += new System.EventHandler(this.UploadButton_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void strClass_SelectedIndexChanged(object sender, System.EventArgs e)
{
string strClass;
strClass=this.strClass.SelectedValue;
strStatus.Text = strClass;
ViewPic(this.strClass.SelectedItem.Value);
}
private void UploadButton_Click(object sender, System.EventArgs e)
{
this.SaveImages();
}
}
}