给你个上传图片的实例 <table style="width: 100%"> <tr> <td> <asp:ValidationSummary ID="ValidationSummary1" runat="server" /> <br /> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btn_upload" runat="server" OnClick="btn_upload_Click" Text="Upload" /> <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="FileUpload1" Display="Static" ErrorMessage="You should only can upload image file such as files with .jpg or gif extension" OnServerValidate="Image_validate">*</asp:CustomValidator> </td> </tr> </table> 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lovnov/archive/2009/08/31/4504280.aspx<table style="width: 100%"> <tr> <td> <asp:ValidationSummary ID="ValidationSummary1" runat="server" /> <br /> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btn_upload" runat="server" OnClick="btn_upload_Click" Text="Upload" /> <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="FileUpload1" Display="Static" ErrorMessage="You should only can upload image file such as files with .jpg or gif extension" OnServerValidate="Image_validate">*</asp:CustomValidator> </td> </tr> </table> 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; using System.Drawing;
public partial class practice_FileUpload : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
}
protected void btn_upload_Click(object sender, EventArgs e) { if (Page.IsValid) { string path = @Page.MapPath("User_Edit.aspx").Replace("User_Edit.aspx", "") + "Documents\\"; string s = path + Session["UserName"].ToString(); if (!System.IO.Directory.Exists(path + Session["UserName"].ToString())) {
{
string filePath="~/UploadImages/"+upImage.FileName;
upImage.SaveAs(MapPath(filePath));
}
string save = "aa";
save.Trim(); string filename = DateTime.Now.ToString("yyMMddHHmmssfff") + FileUpload1.FileName;
this.lujing.Text = filename;
savepath = Server.MapPath("~/pic") + "\\" + filename;
save += filename;
FileUpload1.SaveAs(savepath);
<table style="width: 100%"> <tr> <td> <asp:ValidationSummary ID="ValidationSummary1" runat="server" /> <br /> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btn_upload" runat="server" OnClick="btn_upload_Click" Text="Upload" /> <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="FileUpload1" Display="Static" ErrorMessage="You should only can upload image file such as files with .jpg or gif extension" OnServerValidate="Image_validate">*</asp:CustomValidator> </td> </tr> </table>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lovnov/archive/2009/08/31/4504280.aspx<table style="width: 100%"> <tr> <td> <asp:ValidationSummary ID="ValidationSummary1" runat="server" /> <br /> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btn_upload" runat="server" OnClick="btn_upload_Click" Text="Upload" /> <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="FileUpload1" Display="Static" ErrorMessage="You should only can upload image file such as files with .jpg or gif extension" OnServerValidate="Image_validate">*</asp:CustomValidator> </td> </tr> </table>
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;
using System.Drawing;
public partial class practice_FileUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_upload_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string path = @Page.MapPath("User_Edit.aspx").Replace("User_Edit.aspx", "") + "Documents\\";
string s = path + Session["UserName"].ToString();
if (!System.IO.Directory.Exists(path + Session["UserName"].ToString()))
{
System.IO.Directory.CreateDirectory(path + Session["UserName"].ToString());
}
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("~/Seeker/Documents/" + Session["UserName"].ToString() + "/" + this.FileUpload1.FileName));
}
}
}
protected void Image_validate(object source, ServerValidateEventArgs args)
{
string fileExt = Path.GetExtension(FileUpload1.FileName).ToLower();
string fileName = Path.GetFileName(FileUpload1.FileName);
if (fileExt != ".jpg" && fileExt != ".gif")
{
args.IsValid = false;
}
}
protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
{
Bitmap bmIP = new Bitmap(FileUpload1.PostedFile.InputStream);
if (bmIP.Width > 100 | bmIP.Height > 100)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
}
<table style="width: 100%"> <tr> <td> <asp:ValidationSummary ID="ValidationSummary1" runat="server" /> <br /> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btn_upload" runat="server" OnClick="btn_upload_Click" Text="Upload" /> <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="FileUpload1" Display="Static" ErrorMessage="You should only can upload image file such as files with .jpg or gif extension" OnServerValidate="Image_validate">*</asp:CustomValidator> </td> </tr> </table>
{
Bitmap bmIP = new Bitmap(FileUpload1.PostedFile.InputStream);
if (bmIP.Width > 100 | bmIP.Height > 100)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}