上传文件

代码
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        
try
        {
            
if (this.CheckUpload())
            {

                
//文件儲存路徑
                string strDQAManualFolder = Server.MapPath(@"~\DOC\Manual");
                
//寫入table
                int intLastDotIndex = fudManual.FileName.LastIndexOf('.');
                
string strFileName = fudManual.FileName.Substring(0, intLastDotIndex);
                
string strFileType = fudManual.FileName.Substring(intLastDotIndex + 1, fudManual.FileName.Length - (intLastDotIndex + 1));
                
string strGuid = Guid.NewGuid().ToString();
                
this.InsertTable(strFileName, strGuid, strFileType, this.ddlManualUser.SelectedItem.Text, this.ddlManualDesc.SelectedItem.Text, this.ddlLanguageVersion.SelectedItem.Text, this.currentUser.EngName);
                
//上傳檔案 if 上傳失敗 DELETE 
                String Message = UploadFile(fudManual.FileName, strDQAManualFolder, strFileType, strGuid, fudManual);

                
if (Message != "File uploaded successfully")
                {
                    
this.DeleteField(this.ddlManualUser.SelectedItem.Text, this.ddlManualDesc.SelectedItem.Text, this.ddlLanguageVersion.SelectedItem.Text);
                    
this.ShowMessage(Message);
                }
                
else
                {
                    
this.ShowMessage("Upload Successfully!");
                    
this.ddlManualUser.SelectedValue = "";
                    
this.ddlManualDesc.SelectedValue = "";
                    
this.ddlLanguageVersion.SelectedValue = "";
                }
                gdvManual.EditIndex 
= -1;
                gdvManual.DataBind();
            }

        }
        
catch (Exception ex)
        {
            
this.ShowMessage("Submit function exception!");
            Response.Write(ex.ToString());
        }
    }

    
protected bool CheckUpload()
    {
        
//Check 必填
        if (this.ddlManualUser.SelectedValue == "")
        {
            
this.ddlManualUser.Focus();
            
this.ShowMessage("Please select Manual User!");
            
return false;
        }
        
if (this.ddlManualDesc.SelectedValue == "")
        {
            
this.ddlManualDesc.Focus();
            
this.ShowMessage("Please select Manual Description!");
            
return false;
        }
        
if (this.ddlLanguageVersion.SelectedValue == "")
        {
            
this.ddlLanguageVersion.Focus();
            
this.ShowMessage("Please select Language Version!");
            
return false;
        }
        
if (fudManual.FileName.Length == 0)
        {
            fudManual.Focus();
            
this.ShowMessage("Please select a file!");
            
return false;
        }
        
return  true;
    }

    
public string UploadFile(string strFileName, string strFolderName, string strFileType, string strGuid, FileUpload objDQAManualFileUpload)
    {
        
if (strFileName == "")
        {
            
return "Invalid filename supplied";
        }

        
if (objDQAManualFileUpload.PostedFile.ContentLength == 0)
        {
            
return "Invalid file content";
        }

        strFileName 
= System.IO.Path.GetFileName(strFileName);

        
if (strFolderName == "")
        {
            
return "Path not found";
        }

        
try
        {
            
int MaxFileSize = int.Parse(ConfigurationManager.AppSettings["MaxUploadFileSize"]);
            
if (objDQAManualFileUpload.PostedFile.ContentLength <= MaxFileSize)
            {
                
string fileNewName = strFolderName + "\\" + strGuid + "." + strFileType;
                objDQAManualFileUpload.PostedFile.SaveAs(fileNewName);

                
return "File uploaded successfully";
            }
            
else
            {
                
return "Unable to upload,file exceeds maximum limit";
            }
        }
        
catch (UnauthorizedAccessException ex)
        {
            
return ex.Message + "Permission to upload file denied";
        }
    }

    
protected void InsertTable(string fileName,string guid,string fileType,string manualUser,string manualDesc,string version,string createUser)
    {
        StringBuilder sql 
= new StringBuilder();
        sql.Append(
"Insert into DQA_MANUAL(FILENAME,GUID_FILENAME,TYPENAME,MANUAL_USER,MANUAL_DESCRIPTION,LANGUAGE_VERSION,CREATE_USER,CREATE_DATE,ACTIVE) ");
        sql.Append(
"Values( N'" + fileName + "','" + guid + "','" + fileType + "','" + manualUser + "','" + manualDesc + "','" + version + "','" + createUser + "','" + DateTime.Now.ToString("yyyy-MM-dd  hh:mm:ss"+ "',1) ");
        DbCommand dc 
= DB.GetSqlStringCommand(sql.ToString());
        DB.ExecuteNonQuery(dc);
    }

 

posted on 2009-12-15 14:11  AndyCai  阅读(178)  评论(0编辑  收藏  举报

导航