记录一下ASP.NET上传代码

来源:互联网 发布:javascript与html区别 编辑:程序博客网 时间:2024/06/05 00:32

 

点击上传

Code:
  1. protected void imgBtnUP_Click(object sender, ImageClickEventArgs e)   
  2.    {   
  3.        //string path = FileUpload1.PostedFile.FileName;   
  4.        //string ImgName = path.Substring(path.LastIndexOf("//") + 1);   
  5.        //string ServerPath = Server.MapPath("DropBox/") + ImgName;   
  6.        //FileUpload1.PostedFile.SaveAs(ServerPath);   
  7.        try  
  8.        {   
  9.            if (FileUpload1.PostedFile.FileName != "")   
  10.            {   
  11.                string FilePath = FileUpload1.PostedFile.FileName;//取得文件名(抱括路径)里最后一个"."的索引   
  12.                string FileNames = FilePath.Substring(FilePath.LastIndexOf("//") + 1);   
  13.                string FileExtend = FilePath.Substring(FilePath.LastIndexOf(".") + 1);   
  14.                string FileSize = FileUpload1.PostedFile.ContentLength.ToString();   
  15.                if (!(FileExtend == "xls" || FileExtend == "XLS" || FileExtend == "DOC" || FileExtend == "doc" || FileExtend == "txt" || FileExtend == "TXT" || FileExtend == "rar" || FileExtend == "RAR"))   
  16.                {   
  17.                    BLL.Pub.Show(this,"文件格式不支持,请把要上传的文件打包!"); ;   
  18.                    return;   
  19.                }   
  20.                string ServerPath = Server.MapPath("DropBox/") + FileNames;   
  21.                FileUpload1.PostedFile.SaveAs(ServerPath);   
  22.                MDropBox.UpName = FileNames;   
  23.                MDropBox.UpSize = Convert.ToInt32(FileSize);   
  24.                MDropBox.UpTime = DateTime.Now;   
  25.                MDropBox.UpUrl = ServerPath;   
  26.                //MDropBox.UId   
  27.                BDropBox.Add(MDropBox);   
  28.                BLL.Pub.Show(this,"文件上传成功!");   
  29.            }   
  30.        }   
  31.        catch  
  32.        {   
  33.            BLL.Pub.Show(this,"添加错误!");   
  34.        }   
  35.    }  

 

解释一下:FileUpload1上传控件名

33行调用公共方法,在前面有写过student.csdn.net/space.php

27行ADD方法:

Code:
  1. /// <summary>   
  2.     ///  增加一条数据   
  3.     /// </summary>   
  4.     public int Add(Model.tbDropBox model)   
  5.     {   
  6.         int rowsAffected;   
  7.         SqlParameter[] parameters = {   
  8.                 new SqlParameter("@UpId", SqlDbType.Int,4),   
  9.                 new SqlParameter("@UpName", SqlDbType.NVarChar,100),   
  10.                 new SqlParameter("@UpTime", SqlDbType.DateTime),   
  11.                 new SqlParameter("@UpUrl", SqlDbType.NVarChar,200),   
  12.                 new SqlParameter("@UId", SqlDbType.Int,4),   
  13.                 new SqlParameter("@UpSize", SqlDbType.Int,4)};   
  14.         parameters[0].Direction = ParameterDirection.Output;   
  15.         parameters[1].Value = model.UpName;   
  16.         parameters[2].Value = model.UpTime;   
  17.         parameters[3].Value = model.UpUrl;   
  18.         parameters[4].Value = model.UId;   
  19.         parameters[5].Value = model.UpSize;   
  20.   
  21.         DbHelperSQL.RunProcedure("tbDropBox_ADD",parameters,out rowsAffected);   
  22.         return (int)parameters[0].Value;   
  23.     }  

 

 

 

21行“tbDropBox_ADD”在SQL中存储过程写

Code:
  1. CREATE PROCEDURE [dbo].[tbDropBox_ADD]   
  2. @UpId int output,   
  3. @UpName nvarchar(100),   
  4. @UpTime datetime,   
  5. @UpUrl nvarchar(200),   
  6. @UId int,   
  7. @UpSize int  
  8.   
  9.  AS    
  10.     INSERT INTO [tbDropBox](   
  11.     [UpName],[UpTime],[UpUrl],[UId],[UpSize]   
  12.     )VALUES(   
  13.     @UpName,@UpTime,@UpUrl,@UId,@UpSize   
  14.     )   
  15.     SET @UpId = @@IDENTITY   
  16.   
  17.   
  18. GO   

 

 

原创粉丝点击