上传浏览框
<input id="file1" runat="server" class="inputBox" style="width: 289px" type="file" />
cs
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;
public partial class hr_member_Perpic : System.Web.UI.Page
{
protected string imgurl;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["id"] != null && Session["id"] != "")
{
if (Session["id"] == null)
{
Response.Write("Read ID error");
Response.Redirect("/hr/login.aspx");
}
if (!Page.IsPostBack)
{
Image1.ImageUrl = "~/HR/" + Session["PIC"].ToString();
}
}
else
{
Response.Write("<script>alert('请重新登陆!');self.location='/hr/login.aspx';</script>");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string res,SQL;
upload up = new upload();
res = up.Up(file1, "../UploadFile/");
this.Label1.Visible = true;
this.Label1.Text = up.Resup[Convert.ToInt32(res)];
this.pic.Text = up.Jobs;
Session.Add("PIC", up.Jobs);
Image1.ImageUrl = "~/HR/" + Session["PIC"].ToString();
SQL = "update Job_Person set pic='" + up.Jobs + "' where id='" + Session["id"].ToString() + "'";
SqlData sd = new SqlData();
sd.ExceSQL(SQL);
}
}
upload类
using System;
using System.IO;
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;
/// <summary>
/// upload 的摘要说明
/// </summary>
public class upload
{
public upload()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string[] Resup ={ "上传失败或指定的文件不存在", "图片大于200K,重新传图片!", "格式不对,限制上传(只允许gif/jpg格式文件)!", "上传成功!" };
public string s = string.Empty;
public string Jobs = string.Empty;
public string Up(System.Web.UI.HtmlControls.HtmlInputFile File2, string Pa)
{
//
// TODO: 在此处添加构造函数逻辑
//
if (File2.PostedFile.ContentLength.ToString() == "0")
{
return "0";
}
else
{
//获取文件名称
string ss;
ss = System.DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "") + Path.GetExtension(File2.PostedFile.FileName);
if (File2.PostedFile.ContentLength / 1024 > 200)
{ return "1"; }
else
{
string ty = File2.PostedFile.ContentType;
if (ty == "image/gif" || ty == "image/pjpeg")
{
File2.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(Pa) + ss);
s = "/manager/Uploads/" + ss;
Jobs = "UploadFile/" + ss;
return "3";
//Up= ss;
}
else
{ return "2"; }
}
}
}
}