點擊上傳
Code:
- protected void imgBtnUP_Click(object sender, ImageClickEventArgs e)
- {
- //string path = FileUpload1.PostedFile.FileName;
- //string ImgName = path.Substring(path.LastIndexOf("//") + 1);
- //string ServerPath = Server.MapPath("DropBox/") + ImgName;
- //FileUpload1.PostedFile.SaveAs(ServerPath);
- try
- {
- if (FileUpload1.PostedFile.FileName != "")
- {
- string FilePath = FileUpload1.PostedFile.FileName;//取得檔案名稱(抱括路徑)裡最後一個"."的索引
- string FileNames = FilePath.Substring(FilePath.LastIndexOf("//") + 1);
- string FileExtend = FilePath.Substring(FilePath.LastIndexOf(".") + 1);
- string FileSize = FileUpload1.PostedFile.ContentLength.ToString();
- if (!(FileExtend == "xls" || FileExtend == "XLS" || FileExtend == "DOC" || FileExtend == "doc" || FileExtend == "txt" || FileExtend == "TXT" || FileExtend == "rar" || FileExtend == "RAR"))
- {
- BLL.Pub.Show(this,"檔案格式不支援,請把要上傳的檔案打包!"); ;
- return;
- }
- string ServerPath = Server.MapPath("DropBox/") + FileNames;
- FileUpload1.PostedFile.SaveAs(ServerPath);
- MDropBox.UpName = FileNames;
- MDropBox.UpSize = Convert.ToInt32(FileSize);
- MDropBox.UpTime = DateTime.Now;
- MDropBox.UpUrl = ServerPath;
- //MDropBox.UId
- BDropBox.Add(MDropBox);
- BLL.Pub.Show(this,"檔案上傳成功!");
- }
- }
- catch
- {
- BLL.Pub.Show(this,"添加錯誤!");
- }
- }
解釋一下:FileUpload1上傳控制項名
33行調用公用方法,在前面有寫過student.csdn.net/space.php
27行ADD方法:
Code:
- /// <summary>
- /// 增加一條資料
- /// </summary>
- public int Add(Model.tbDropBox model)
- {
- int rowsAffected;
- SqlParameter[] parameters = {
- new SqlParameter("@UpId", SqlDbType.Int,4),
- new SqlParameter("@UpName", SqlDbType.NVarChar,100),
- new SqlParameter("@UpTime", SqlDbType.DateTime),
- new SqlParameter("@UpUrl", SqlDbType.NVarChar,200),
- new SqlParameter("@UId", SqlDbType.Int,4),
- new SqlParameter("@UpSize", SqlDbType.Int,4)};
- parameters[0].Direction = ParameterDirection.Output;
- parameters[1].Value = model.UpName;
- parameters[2].Value = model.UpTime;
- parameters[3].Value = model.UpUrl;
- parameters[4].Value = model.UId;
- parameters[5].Value = model.UpSize;
-
- DbHelperSQL.RunProcedure("tbDropBox_ADD",parameters,out rowsAffected);
- return (int)parameters[0].Value;
- }
21行“tbDropBox_ADD”在SQL中預存程序寫
Code:
- CREATE PROCEDURE [dbo].[tbDropBox_ADD]
- @UpId int output,
- @UpName nvarchar(100),
- @UpTime datetime,
- @UpUrl nvarchar(200),
- @UId int,
- @UpSize int
-
- AS
- INSERT INTO [tbDropBox](
- [UpName],[UpTime],[UpUrl],[UId],[UpSize]
- )VALUES(
- @UpName,@UpTime,@UpUrl,@UId,@UpSize
- )
- SET @UpId = @@IDENTITY
-
-
- GO