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 xueshengzuoye: System.Web.UI.Page
{
public static string fname;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request["ID"] != null)
{
InitData();
}
}
}
private void FileUpLoad()
{
if (UploadFile.Value != null)
{
string nam = UploadFile.PostedFile.FileName;
//取得文件名(抱括路径)里最后一个"."的索引
int i = nam.LastIndexOf(".");
//取得文件扩展名
string newext = nam.Substring(i);
//这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
DateTime now = DateTime.Now;
string newname = now.ToFileTimeUtc() + UploadFile.PostedFile.ContentLength.ToString();
//保存文件到你所要的目录,这里是IIS根目录下的uploadfiles目录
//注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
UploadFile.PostedFile.SaveAs(Server.MapPath("UpLoad/" + newname + newext));
//得到这个文件的相关属性:文件名,文件类型,文件大小
fname = "UpLoad/" + newname + newext;
}
}
protected void InitData()
{
string id = Request["ID"].ToString();
try
{
string SQL = "select * from TB_xueshengzuoye where ID='" + id + "'";
DataTable dt = DBHelper.GetDataSet(SQL).Tables[0];
if (dt.Rows.Count > 0)
{
this.TextBox1.Text = dt.Rows[0]["ZuoYeName"].ToString();
}
}
catch
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (UploadFile.Value != null && UploadFile.Value != "")
{
FileUpLoad();
}
string SQL = "";
bool iRet = false;
if (Request["ID"] != null)
{
string id = Request["ID"].ToString();
SQL = "update TB_xueshengzuoye set ZuoYeName='" + this.TextBox1.Text + "',URl='" + fname + "' where ID='" + id + "'";
iRet = DBHelper.ExecSql(SQL);
}
else
{
SQL = "insert into TB_xueshengzuoye(ZuoYeName,URl,AddUser ) values('" + this.TextBox1.Text + "','" + fname + "','" + Session["username"].ToString() + "')";
iRet = DBHelper.ExecSql(SQL);
}
if (iRet)
{
Response.Write("<script>alert('恭喜您,操作成功!');document.location=document.location;</script>");
}
else
{
Response.Write("<script>alert('对不起,操作失败!');document.location=document.location;</script>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
this.TextBox1.Text = "";
}
}
为什么一上传文件就提示对不起,操作失败!