天天看點

HTML控件之圖檔上傳

1.file控件

2.在項目檔案中建立一個檔案夾up

 //檔案上傳

        //擷取上傳檔案的全路徑(D:/圖檔/桌面/windows701.bmp)

        string fullFileName=this.File1.PostedFile.FileName;

        //截取全路徑使得路徑隻剩下檔案名(windows701.bmp)

        string fileName = fullFileName.Substring(fullFileName.LastIndexOf("//")+1);

        //将上傳檔案的檔案名加到伺服器上要存放的檔案夾up的後,這樣就得到了上傳檔案的絕對路徑(C:/Documents and Settings/jetlei/My Documents/Visual Studio 2008/Projects/WebSite1/up/windows701.bmp)

        this.File1.PostedFile.SaveAs(Server.MapPath("up")+"//"+fileName);

        //圖檔上傳,因為圖檔有不同個格式,是以在上傳圖檔前一定要獲得圖檔的格式,并判斷

        string fullFileName = this.File1.PostedFile.FileName;

        string fileName = fullFileName.Substring(fullFileName.LastIndexOf("//") + 1);

        //擷取最後一個點後面的字元串

        string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);

        if (type == "jpg" || type == "bmp" || type == "gif")

        {

            this.File1.PostedFile.SaveAs(Server.MapPath("up") + "//" + fileName);

            //顯示圖檔在image控件中

            this.Image1.ImageUrl = "up/" + fileName;

        }

        else

        {

            Response.Write("<script>alert('你選擇的圖檔格式錯誤')<script>");

        }