Input(file) 控制項的簡單使用!

來源:互聯網
上載者:User
 Input(file) 控制項主要用來向伺服器端上傳檔案,下面我們通過一個簡單上傳圖片的例子說明他的使用方法:

開啟.NET 在頁面上拖人一個 Input(file)控制項,點擊右鍵,選擇“作為伺服器控制項運行”,然後拖一個 button 和一個image

在button 事件中加入如下代碼:

  protected void Button1_Click(object sender, EventArgs e)
    {
        string fullname=this.File1.PostedFile.FileName;              //擷取檔案在用戶端的完整路徑和檔案名稱
        string name=fullname.Substring(fullname.LastIndexOf("\\")+1);     // 字串截取函數取得檔案名稱
        string type = name.Substring(name.LastIndexOf(".") + 1);       // 字串截取函數取得檔案尾碼名
        if(type=="jpg" || type=="bmp"||type=="gif")                // 判斷是否為圖片
        {
        this.File1.PostedFile.SaveAs(Server.MapPath("up")+"\\"+name );          //上傳到伺服器UP目錄
        this.Image1.ImageUrl = "up/" + name;             //圖片顯示
        }
        else
        {
        Response.Write("<script language='javascript'>alert('你選擇的圖片格式錯誤!')</script>");
        }
    }
在.net2005上測試通過,如果是其他格式的,改變相應的尾碼名即可;為防止重名可把 name  變數用隨機函數和日期混合重新命名!

==================================================================
附:indexof()    substring()   函數用法:

indexof() :在字串中從前向後定位字元和字串;所有的傳回值都是指在字串的絕對位置,如為空白則為- 1

string test="asdfjsdfjgkfasdsfsgfhgjgfjgdddd";

  test.indexof('d')      =2           //從前向後 定位 d 第一次出現的位置
  test.indexof('d',1)    =2          //從前向後 定位 d 從第三個字串 第一次出現的位置
  test.indexof('d',5,2)  =6     //從前向後 定位 d 從第5 位開始查,查2位,即 從第5位到第7位;

lastindexof() :在字串中從後向前定位字元和字串;、
用法和 indexof() 完全相同。

下面介紹  IndexOfAny ||lastindexofany

他們接受字元數組做為變元,其他方法同上,返回數組中任何一個字元最早出現的下標位置

如下

        char[] bbv={'s','c','b'};
        string abc = "acsdfgdfgchacscdsad";
        
        Response.Write(abc.IndexOfAny(bbv))=1
        Response.Write(abc.IndexOfAny(bbv, 5))=9
        Response.Write(abc.IndexOfAny(bbv, 5, 3))=9

lastindexofany 同上。
====================================================================
substring() 用法

string a="aadsfdjkfgklfdglfd"

a.substring(5)     //截取從第五位以後的所有字串

a.substring(0,5)    //截取從第0到第5 以後的所有字串

...........................................結束。。。。。。。。

聯繫我們

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