日期:2014-05-18  浏览次数:20464 次

管理员上传文件模块
做了一个以管理员身份上传文件的页面,后台代码如下:
public partial class UploadFile : System.Web.UI.Page
{
  string stradmin;
  protected void Page_Load(object sender, EventArgs e)
  {
  stradmin = Request.QueryString["AdminName"]; 这一句也不能获取AdminName的值
  }
  protected void btnupload_click(object sender, EventArgs e)
  {
  string strfilename;
  string strfiletype;
  string strtime;
   
  Random ran = new Random();
  string strfileisn = DateTime.Now.ToString("yyyymmddhhmmss") + ran.Next (0, 999);
  strtime = DateTime.Now.ToString();
  string strSql;
  if (FileUpload1.PostedFile.FileName != null)
  { //存在上传文件
  strfilename = FileUpload1.PostedFile .FileName ;
  strfiletype = strfilename.Substring (strfilename.LastIndexOf (".") + 1);
  strfilename=strfilename.Substring (strfilename.LastIndexOf ("\\") + 1);
  SqlDataAdapter sda = new SqlDataAdapter();
  clsDB dbo = new clsDB();
  strSql = "select * from PatentFile";
  sda = dbo.ReturnDataAdapter(strSql);
  DataSet ds = new DataSet("PatentFile");
  sda.Fill(ds, "PatentFile");
  DataRow dr = ds.Tables["PatentFile"].NewRow();
  System.IO.Stream fs = FileUpload1.PostedFile.InputStream;

  int fileLength = FileUpload1.PostedFile.ContentLength;

  Byte[] filedata = new Byte[fileLength];

  fs.Read(filedata, 0, fileLength);
  fs.Close();
  //判断文件的大小
  int i;
  i = filedata.Length;
  if (i>0)
  {
  dr["FileIsn"] = strfileisn;
  dr["FileName"] = strfilename;
  dr["FileType"] = strfiletype;
  dr["FileSize"] = filedata.Length;  
  dr["FileContent"] = filedata;
  dr["AdminName"] = stradmin;
  dr["UploadTime"] = strtime; 
  ds.Tables["PatentFile"].Rows.Add(dr);
  try
  {
  sda.Update(ds, "PatentFile");
  lblresult.Text = strfilename + "文件上传成功!";
  }
  catch
  {
  lblresult.Text = "文件上传失败!";
  }

  sda.Dispose();
  dbo.killMe();  
  } 
  else
  {
  lblresult.Text ="请选择要上传的文件!";
  }
  }
  }
}

其中自定义类clsDB大致如下:public class clsDB
{
  public SqlConnection Connection;
  public clsDB()
  {
  Connection =new SqlConnection(Configuratio