HTML控件之图片上传

来源:互联网 发布:兼职淘宝刷手会坐牢吗 编辑:程序博客网 时间:2024/06/02 07:55

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>");
        }