ASP.NET小企图片代码
欢迎进入.NET社区论坛,与300万技术人员互动交流 >>进入
ASP.NET小企图片代码:
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Title = “图片小企”;
Image1.AlternateText = “图片尚未小企”;
Label1.Visible = false;
Label2.Visible = false;
}
protected void file_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string name = FileUpload1.PostedFile.FileName;
FileInfo file = new FileInfo(name);
string fileName = file.Name;
string webFilePath = Server.MapPath(“images/” + fileName);
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == “image/bmp” || fileContentType == “image/gif” || fileContentType == “image/pjpeg”)
{
if (!File.Exists(webFilePath))
{
try
{
FileUpload1.SaveAs(webFilePath);
Label1.Visible = true;
Label2.Visible = true;
Label1.Text = “文件”“ + fileName + ”“成功小企,” + “文件大小为:” + FileUpload1.PostedFile.ContentLength + “字节”;
Image1.Visible = true;
Image1.ImageUrl = “images/” + fileName;
Image1.Width = 200;
Image1.Height = 150;
Label2.Text = “按200×150大小显示”;
}
catch (Exception ex)
{
Label1.Visible = true;
Label1.Text = “文件小企失败,失败原因:” + ex.Message;
}
}
else
{
Label1.Visible = true;
Label1.Text = “文件已经存在,请重命名后小企”;
}
}
else
{
Label1.Visible = true;
Label1.Text = “文件类型不符,只能小企*.bmp,*.jpg,*.gif类型的文件”;
}
}
else
{
Label1.Visible = true;
Label1.Text = “请选择文件或输入文件路径及名称!”;
}
}
}
【责编:peter】