1 <form id="form1" runat="server"> 2 <div> 3 文件名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 4 <asp:Image ID="Image1" runat="server" /> 5 <asp:FileUpload ID="FileUpload1" runat="server" /> 6 <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /> 7 </div> 8 </form> 9 10 11 12 protected void Button2_Click(object sender, EventArgs e) 13 { 14 if (FileUpload1.HasFile) 15 { 16 HttpPostedFile hf = FileUpload1.PostedFile; 17 string filename = hf.FileName;//全路径:F:asp456.jpg 18 string fname = Path.GetFileName(filename);//文件名:456 19 string ext = Path.GetExtension(fname).ToLower();//获取拓展名:.jpg 20 Response.Write(fname + "<br/>" + ext); 21 string[] exts = { ".jpg", ".jpeg", ".png", ".bmp", ".gif" }; 22 bool tag = false; 23 foreach (string item in exts) 24 { 25 if (item == ext) 26 { 27 tag = true; 28 break; 29 } 30 } 31 if (tag) 32 { 33 if (hf.ContentLength < 1024 * 1024) 34 { 35 string uppath = "upload/"; 36 string dir = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day; 37 string dirpath = Server.MapPath(uppath + dir); 38 if (!Directory.Exists(dirpath)) 39 { 40 Directory.CreateDirectory(dirpath); 41 } 42 if (TextBox1.Text != "") 43 { 44 fname = TextBox1.Text + ext; 45 } 46 hf.SaveAs(dirpath + fname); 47 Image1.ImageUrl = uppath + dir + fname; 48 Image1.Width = 400; 49 Image1.Height = 300; 50 } 51 else 52 { 53 Response.Write("<script>alert('上传文件太大')</script>"); 54 } 55 } 56 } 57 else 58 { 59 Response.Write("<script>alert('选择上传文件')</script>"); 60 } 61 } 62 63 64 65 66 ---------------------------------------------------------------------------------------------------------------------------------- 67 <form id="form1" runat="server"> 68 <div>Upload 69 <asp:Image ID="Image1" runat="server" /></br> 70 <span style="color:#4cff00" >图片预览</span></br> 71 <asp:TextBox ID="txtCover" runat="server" ReadOnly="true"></asp:TextBox></br> 72 <span style="color:#4cff00" >图片上传的路径</span></br> 73 <asp:FileUpload ID="imgupload" runat="server" /> 74 <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" /> 75 76 </div> 77 </form> 78 79 protected void btnUpload_Click(object sender, EventArgs e) 80 { 81 #region 82 //是否有文件加载 83 if (imgupload.PostedFile.FileName != "")//不为空,证明有文件上传 84 { 85 //获取图片路径和文件名 imgPath=本地路径\本地二级路径\文件名.扩展名 86 string imgPath = imgupload.PostedFile.FileName;//imgPath=D:ImageGroup$)91{Y9@Q(R@7@2M6APE_ZI.png 87 //截取图片扩展名 +1???“1”是什么含义 88 string extPath = imgPath.Substring(imgPath.LastIndexOf(".") + 1);//png jpg bmp 89 //判断文件类型 为什么要用装换大小写?ToLower 90 if (extPath.ToLower() != "bmp" && extPath.ToLower() != "jpg" && extPath.ToLower() != "png") 91 { 92 Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script language=javascript>alert('选择文件类型JPG,bmp,PNG')</script>"); 93 94 } 95 else 96 { 97 //思路:1.先定义好物理路径(文件 要保存到的文件夹)2.sPath+上传图片的xx+.jpg 98 // 得到服务器的上传路径 99 string sPath = Server.MapPath("/UploadFiles/upload");// MapPath物理路径就是右斜/ 100 //保存到服务器 sPath=UploadFilesupload 101 imgupload.PostedFile.SaveAs(sPath + @"" + imgPath.Substring(imgPath.LastIndexOf(@"") + 1));//存储虚拟路径+图片名字 102 sPath = sPath.Substring(sPath.LastIndexOf(@"") + 1); //sPath = 截取文件夹里的upload; 103 //在这里Spath被重置为upload 104 // 接下来就是要在TxtBox中显示路径 105 this.txtCover.Text = sPath + "/" + imgPath.Substring(imgPath.LastIndexOf(@"") + 1);//文件夹里的upload+xx.jpg; 106 // 这里txtCover的值为upload文件名.扩展名 107 Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script language=javascript>alert('上传成功')</script>"); 108 Image1.ImageUrl = "/UploadFiles/" + this.txtCover.Text;//?upload/$@B0)U`]TDA@MKRDW15BJQR.jpg 109 } 110 } 111 112 else 113 { 114 Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script language=javascript>alert('请选择图片')</script>"); 115 } 116 #endregion