WebForm1.aspx
WebForm1function addFile()
{
var str = ''
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
function writeMMS() {
mymms.document.open();
mymms.document.writeln("");
mymms.document.writeln("
");mymms.document.writeln("
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("");
mymms.document.writeln("");
mymms.document.writeln("
");mymms.document.writeln("");
mymms.document.writeln("");
mymms.document.writeln("");
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);
}
彩信精灵(上传多张自己的图片,然后编辑,加上文字)
类别
所有图片
山水图片
人物图片
QQ图片
文字图片
其他类别
type=text maxLength=11 size=11 name=mobile> |
//**************************************************************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
{
///
/// WebForm1 的摘要说明。
///
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("上传的文件分别是:
");
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() + "
");
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "
");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "
");
strMsg.Append("上传文件的文件名:" + fileName + "
");
strMsg.Append("上传文件的扩展名:" + fileExtension + "
");
//'可根据扩展名字的不同保存到不同的文件夹
//注意:可能要修改你的文件夹的匿名写入权限。
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);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
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();
}
}
}