zoukankan      html  css  js  c++  java
  • 上传文件

    代码
        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);
        }
  • 相关阅读:
    这就是搜索引擎--读书笔记六--索引的查询
    这就是搜索引擎--读书笔记五--索引的建立与更新
    JavaWeb学习总结第四篇--Servlet开发
    算法帝国--读书笔记
    这就是搜索引擎--读书笔记四--索引基础
    这就是搜索引擎--读书笔记三
    Python学习总结之五 -- 入门函数式编程
    ASP.NET-FineUI开发实践-4(二)
    ASP.NET-FineUI开发实践-4
    ASP.NET-FineUI开发实践-3
  • 原文地址:https://www.cnblogs.com/andycai/p/1624758.html
Copyright © 2011-2022 走看看