用Session當變數使用

來源:互聯網
上載者:User

以下是轉貼“擷取檔案尾碼的演算法...(asp.net+C#)”,還沒試過

======================================================================                                     

 

一般在上傳檔案的時候,都需要擷取檔案的類型,以前都是用一中類型,對與,只上傳一種類型的系統是沒有問題的, 但是,要上傳幾中不同的類型的檔案的時候,就會出現問題,我寫了個擷取檔案尾碼的演算法,今天帖出來,

 //擷取檔案尾碼(類型)
   string filestyle=null;
   //定義一個檔案名稱類型變數;
   
   
   
   //截取檔案的尾碼;
   string AllFileName=this.files.PostedFile.FileName.ToString();
   int str_len,all_len;
   all_len=AllFileName.Length;
   for(int i=0;i<AllFileName.Length;i++)
   {
    if(AllFileName[i].ToString()==".")
    {
              Session["str_len"]=i;
    }
   }
            str_len=int.Parse(Session["str_len"].ToString());
            string sub_str=AllFileName.Substring(str_len,all_len-str_len);
         
   filestyle=sub_str;
         
   
   
   
   
   //上傳圖片
   //隨機檔案名稱
   string filename = null;
   filename = DateTime.Now.ToString();
   filename = filename.Replace(" ", "");
   filename = filename.Replace(":", "");
   filename = filename.Replace("-", "");
   filename=filename;
   string FilePath = null;
   FilePath = Server.MapPath("../uploadFiles") + "//";
   this.files.PostedFile.SaveAs(FilePath + filename + filestyle);

   //設定上傳的檔案名稱的senssion
   Session["filename"]=filename;
   Response.Write("<script>window.alert('上傳成功');window.location('history.go(-1)')</script>");

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.