記錄一下ASP.NET上傳代碼

來源:互聯網
上載者:User

 

點擊上傳

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   

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.