今天要做一个上传图片的程序,把相关的一点代码粘出来,分享一下。
string getName = Session["useName"].ToString(); string root = @"E:/taobaocheng/product/"+getName+"//"; string savePath = root; int size; string Imageformat; if (!Directory.Exists(root)) { Directory.CreateDirectory(root); }
if (this.FileUpload1.HasFile) { size = this.FileUpload1.PostedFile.ContentLength; if (size > 150000)//图片大小大于150k { this.Label1.Text = "图片不能大于150K"; //this.Response.End(); } else { Imageformat = (this.FileUpload1.FileName.Substring(this.FileUpload1.FileName.LastIndexOf("."))).ToLower(); if (Imageformat == ".jpg" || Imageformat == ".bmp" || Imageformat == ".png" || Imageformat == ".gif")//限制图片格式 { savePath += this.FileUpload1.FileName; this.FileUpload1.SaveAs(savePath); this.Label1.Text = "文件上传成功"; } else { this.Label1.Text = "你上传格式不正确,正确格式:.jpg、.png、.bmp、.gif"; } } } else { this.Label1.Text = "请选择指定文件"; } } |