protected void Button1_Click(object sender, EventArgs e) { byte[] bt = SetImageToByteArray(FileUpload1);//将上传文件转换成byte[]数组 if (bt != null) { string programPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();//获取当前项目路径 string picname = DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(1000, 9999) + ".jpg"; string path = programPath +"image/"+ picname;//文件存储路径 MemoryStream ms = new MemoryStream(bt);//将byte[]数组存入内存流 FileStream fs = new FileStream(path, FileMode.Create);//创建文件流 ms.WriteTo(fs);//将内存流写入文件流 System.Drawing.Image image = System.Drawing.Image.FromStream(fs, true);//将文件流转成image对象类型 image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);//存储对象 ms.Close(); fs.Close(); ms = null; fs = null; Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('ok!')</script>"); } } public byte[] SetImageToByteArray(FileUpload FileUpload1) { string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();//获取文件类型 if (fileExtension == ".jpg") { Stream stream = FileUpload1.PostedFile.InputStream; byte[] photo = new byte[FileUpload1.PostedFile.ContentLength]; stream.Read(photo, 0, FileUpload1.PostedFile.ContentLength); stream.Close(); return photo; } else { byte[] bt=null; return bt; } }