asp.net2.0 FileUpload控件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class business_uploadimg : System.Web.UI.Page
{
public string filepath = "";
public int count = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
HideControls();
this.firstdel.Attributes.Add("onclick", "return confirm('确认要删除第一张产品图片吗?')");
this.seconddel.Attributes.Add("onclick", "return confirm('确认要删除第二张产品图片吗?')");
this.thirddel.Attributes.Add("onclick", "return confirm('确认要删除第三张产品图片吗?')");
}
}
public void HideControls()
{
firstupload.Visible = false;
secondupload.Visible = false;
thirdupload.Visible = false;
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
string fullfile = FileUpload1.PostedFile.FileName; //获取客户端上的文件的完全限定名称
string imgtype = FileUpload1.PostedFile.ContentType; //获取客户端发送的文件的MIME内容类型
int imgsize = FileUpload1.PostedFile.ContentLength; //获取上载文件的大小(以字节为单位)
string extension = Path.GetExtension(fullfile).ToLower();//
if (fullfile == "" || fullfile == null)
{
Ahcom.Base.MessageObject.ShowMessage(this, "请选择一张产品图片再上传!");
return;
}
else if (extension != ".jpg" && extension != ".jpeg" && extension != ".gif")
{
Ahcom.Base.MessageObject.ShowMessage(this, "请您选择一个有效的文件上传!http://www.cnblogs.com/juan/admin/file://n/支持的图片格式有(.jpg|.jpeg|.gif),http://www.cnblogs.com/juan/admin/file://n/请您转换图片格式后重新上传!");
return;
}
else if (imgsize <= 0)
{
Ahcom.Base.MessageObject.ShowMessage(this, "上传的图片太小,http://www.cnblogs.com/juan/admin/file://n/ 请您选择一张清晰的图片后重新上传!");
return;
}
else if (imgsize > 2048*1024)
{
Ahcom.Base.MessageObject.ShowMessage(this, "您上传的图片总大小超出的最大限制了(2MB)!");
return;
}
else
{
try
{
string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString();
filename += extension;
HiddenField1.Value = filename;
Session["ImageFile1"] = filename;
string path = Server.MapPath("../UpLoadFiels/ProductPic/");
filepath = path + filename;
try
{
this.FileUpload1.PostedFile.SaveAs(filepath);
}
catch (Exception error)
{
Ahcom.Base.MessageObject.ShowMessage(this, "您上传的磁盘没有写入权限,请您设置可写权限! http://www.cnblogs.com/juan/admin/file://n/错误原因:" + error.Message + "");
return;
}
if (extension == ".gif" || extension == ".jpg" || extension == ".jpeg")
{
firstimg.Height = 100;
firstimg.Width = 100;
firstimg.ImageAlign = ImageAlign.AbsMiddle;
firstimg.ImageUrl = "../UpLoadFiels/ProductPic/" + filename;
HideControls();
}
else
{
firstimg.Height = 100;
firstimg.Width = 100;
firstimg.ImageAlign = ImageAlign.AbsMiddle;
firstimg.ImageUrl = "~/images/index/detail_no_pic.gif";
}
}
catch (Exception ex)
{
Ahcom.Base.MessageObject.ShowMessage(this, "上传图片失败!http://www.cnblogs.com/juan/admin/file://n/错误原因:" + ex.Message + "");
return;
}
}
}
protected void firstdel_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(Server.MapPath("../UpLoadFiels/ProductPic/") + HiddenField1.Value) == true)
{
try
{
File.Delete(Server.MapPath("../UpLoadFiels/ProductPic/") + HiddenField1.Value);
firstimg.Height = 100;
firstimg.Width = 100;
firstimg.ImageAlign = ImageAlign.AbsMiddle;
firstimg.ImageUrl = "~/images/index/detail_no_pic.gif";
count = 1;
firstupload.Visible = true;
secondupload.Visible = false;
thirdupload.Visible = false;
}
catch(Exception ex)
{
Ahcom.Base.MessageObject.ShowMessage(this, "删除图片失败,失败原因:"+ex.Message);
return;
}
}
else
{
Ahcom.Base.MessageObject.ShowMessage(this, "删除的图片文件不存在!");
firstimg.Height = 100;
firstimg.Width = 100;
firstimg.ImageAlign = ImageAlign.AbsMiddle;
firstimg.ImageUrl = "~/images/index/detail_no_pic.gif";
}
}
protected void firstup_Click(object sender, EventArgs e)
{
count = 1;
firstupload.Visible = true;
secondupload.Visible = false;
thirdupload.Visible = false;
}
protected void btnsecondup_Click(object sender, EventArgs e)
{
count = 2;
firstupload.Visible = false;
secondupload.Visible = true;
thirdupload.Visible = false;
}
protected void btnthirdup_Click(object sender, EventArgs e)
{
count = 3;
firstupload.Visible = false;
secondupload.Visible = false;
thirdupload.Visible = true;
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string fullfile = FileUpload2.PostedFile.FileName; //获取客户端上的文件的完全限定名称
string imgtype = FileUpload2.PostedFile.ContentType; //获取客户端发送的文件的MIME内容类型
int imgsize = FileUpload2.PostedFile.ContentLength; //获取上载文件的大小(以字节为单位)
string extension = Path.GetExtension(fullfile).ToLower();//
if (fullfile == "" || fullfile == null)
{
Ahcom.Base.MessageObject.ShowMessage(this, "请选择一张产品图片再上传!");
return;
}
else if (extension != ".jpg" && extension != ".jpeg" && extension != ".gif")
{
Ahcom.Base.MessageObject.ShowMessage(this, "请您选择一个有效的文件上传!http://www.cnblogs.com/juan/admin/file://n/支持的图片格式有(.jpg|.jpeg|.gif),http://www.cnblogs.com/juan/admin/file://n/请您转换图片格式后重新上传!");
return;
}
else if (imgsize <= 0)
{
Ahcom.Base.MessageObject.ShowMessage(this, "上传的图片太小,http://www.cnblogs.com/juan/admin/file://n/ 请您选择一张清晰的图片后重新上传!");
return;
}
else if (imgsize > 2048*1024)
{
Ahcom.Base.MessageObject.ShowMessage(this, "您上传的图片总大小超出的最大限制了(2MB)!");
return;
}
else
{
try
{
string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString();
filename += extension;
HiddenField2.Value = filename;
Session["ImageFile"] = filename;
string path = Server.MapPath("../UpLoadFiels/ProductPic/");
filepath = path + filename;
try
{
this.FileUpload2.PostedFile.SaveAs(filepath);
}
catch (Exception err)
{
Ahcom.Base.MessageObject.ShowMessage(this, "您上传的磁盘没有写入权限,请您设置可写权限! http://www.cnblogs.com/juan/admin/file://n/错误原因:" + err.Message + "");
return;
}
if (extension == ".gif" || extension == ".jpg" || extension == ".jpeg")
{
secondimg.Height = 100;
secondimg.Width = 100;
secondimg.ImageAlign = ImageAlign.AbsMiddle;
secondimg.ImageUrl = "../UpLoadFiels/ProductPic/" + filename;
HideControls();
}
else
{
secondimg.Height = 100;
secondimg.Width = 100;
secondimg.ImageAlign = ImageAlign.AbsMiddle;
secondimg.ImageUrl = "~/images/index/detail_no_pic.gif";
}
}
catch (Exception ex)
{
Ahcom.Base.MessageObject.ShowMessage(this, "上传图片失败!http://www.cnblogs.com/juan/admin/file://n/错误原因:" + ex.Message + "");
return;
}
}
}
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
string fullfile = FileUpload3.PostedFile.FileName; //获取客户端上的文件的完全限定名称
string imgtype = FileUpload3.PostedFile.ContentType; //获取客户端发送的文件的MIME内容类型
int imgsize = FileUpload3.PostedFile.ContentLength; //获取上载文件的大小(以字节为单位)
string extension = Path.GetExtension(fullfile).ToLower();//
if (fullfile == "" || fullfile == null)
{
Ahcom.Base.MessageObject.ShowMessage(this, "请选择一张产品图片再上传!");
return;
}
else if (extension != ".jpg" && extension != ".jpeg" && extension != ".gif")
{
Ahcom.Base.MessageObject.ShowMessage(this, "请您选择一个有效的文件上传!http://www.cnblogs.com/juan/admin/file://n/支持的图片格式有(.jpg|.jpeg|.gif),http://www.cnblogs.com/juan/admin/file://n/请您转换图片格式后重新上传!");
return;
}
else if (imgsize <= 0)
{
Ahcom.Base.MessageObject.ShowMessage(this, "上传的图片太小,http://www.cnblogs.com/juan/admin/file://n/ 请您选择一张清晰的图片后重新上传!");
return;
}
else if (imgsize > 2048*1024)
{
Ahcom.Base.MessageObject.ShowMessage(this, "您上传的图片总大小超出的最大限制了(2MB)!");
return;
}
else
{
try
{
string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString();
filename += extension;
HiddenField3.Value = filename;
Session["ImageFile3"] = filename;
string path = Server.MapPath("../UpLoadFiels/ProductPic/");
filepath = path + filename;
try
{
this.FileUpload3.PostedFile.SaveAs(filepath);
}
catch (Exception errmsg)
{
Ahcom.Base.MessageObject.ShowMessage(this, "您上传的磁盘没有写入权限,请您设置可写权限!http://www.cnblogs.com/juan/admin/file://n/错误原因: " + errmsg.Message + "");
return;
}
if (extension == ".gif" || extension == ".jpg" || extension == ".jpeg")
{
thirdimg.Height = 100;
thirdimg.Width = 100;
thirdimg.ImageAlign = ImageAlign.AbsMiddle;
thirdimg.ImageUrl = "../UpLoadFiels/ProductPic/" + filename;
HideControls();
}
else
{
thirdimg.Height = 100;
thirdimg.Width = 100;
thirdimg.ImageAlign = ImageAlign.AbsMiddle;
thirdimg.ImageUrl = "~/images/index/detail_no_pic.gif";
}
}
catch (Exception ex)
{
Ahcom.Base.MessageObject.ShowMessage(this, "上传图片失败!http://www.cnblogs.com/juan/admin/file://n/错误原因:" + ex.Message + "");
return;
}
}
}
protected void seconddel_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(Server.MapPath("../UpLoadFiels/ProductPic/") + HiddenField2.Value) == true)
{
try
{
File.Delete(Server.MapPath("../UpLoadFiels/ProductPic/") + HiddenField2.Value);
secondimg.Height = 100;
secondimg.Width = 100;
secondimg.ImageAlign = ImageAlign.AbsMiddle;
secondimg.ImageUrl = "~/images/index/detail_no_pic.gif";
count = 2;
firstupload.Visible = false;
secondupload.Visible = true;
thirdupload.Visible = false;
}
catch (Exception ex)
{
Ahcom.Base.MessageObject.ShowMessage(this, "删除图片失败,失败原因:" + ex.Message);
return;
}
}
else
{
Ahcom.Base.MessageObject.ShowMessage(this, "删除的图片文件不存在!");
secondimg.Height = 100;
secondimg.Width = 100;
secondimg.ImageAlign = ImageAlign.AbsMiddle;
secondimg.ImageUrl = "~/images/index/detail_no_pic.gif";
}
}
protected void thirddel_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(Server.MapPath("../UpLoadFiels/ProductPic/") + HiddenField3.Value) == true)
{
try
{
File.Delete(Server.MapPath("../UpLoadFiels/ProductPic/") + HiddenField3.Value);
thirdimg.Height = 100;
thirdimg.Width = 100;
thirdimg.ImageAlign = ImageAlign.AbsMiddle;
thirdimg.ImageUrl = "~/images/index/detail_no_pic.gif";
count = 3;
firstupload.Visible = false;
secondupload.Visible = false;
thirdupload.Visible = true;
}
catch (Exception ex)
{
Ahcom.Base.MessageObject.ShowMessage(this, "删除图片失败,失败原因:" + ex.Message);
return;
}
}
else
{
Ahcom.Base.MessageObject.ShowMessage(this, "删除的图片文件不存在!");
thirdimg.Height = 100;
thirdimg.Width = 100;
thirdimg.ImageAlign = ImageAlign.AbsMiddle;
thirdimg.ImageUrl = "~/images/index/detail_no_pic.gif";
}
}
}