asp.2.0/C#关于FileUpload控件上传图片
try { string name = FileUpload1。FileName; string size = FileUpload1。PostedFile。ContentLength。
ToString(); string type = name。Substring(name。LastIndexOf("。") + 1)。ToLower(); if (type == "jpg" || type == "png" || type == "bmp" || type == "gif" || type == "jpeg") { try { ImgUploads iu = new ImgUploads(DropDownList1。
SelectedItem。Text); String id=iu。GetMaxId();//获取数据库最大ID号用来命名图片 String ReName = id + "。
" + type;//图片重命名 String Ipath = Server。MapPath("~/hy/upimg") + "\\" + ReName;//文件实际路径 FileUpload1。
SaveAs(Ipath);//上传到图片目录 String Tpath = Server。MapPath("~/hy/thumbar") + "\\" + ReName;//缩略图路径 PictureSlightly。
MakeImage(Ipath,Tpath,100,130,"hw");//生成缩略图//上传到数据库 iu。UpImg(Convert。ToInt32(id),DropDownList1。SelectedItem。
Text,ReName。ToString()); Label1。Text = "上传成功,图片大小:" + size + "B"; umbar。Src = "~/hy/thumbar/" + ReName; } catch(Exception ex) { Page。
RegisterStartupScript("error", "<script>alert('图片上传失败,请重试!"+ex。Message。ToString()+"');</script>"); } } else { Page。
RegisterStartupScript("error", "<script>alert('图片格式不对!');</script>"); } } catch { Page。
RegisterStartupScript("error", "<script>alert('图片上传失败,请重试!');</script>"); } }。
其实你不用管图片名称在数据库中是否存在,建个ID字段 ,作为表的主健嘛! Stream imgdatastream = this。FileUpload_Pic。PostedFile。InputStream;//文件上穿控件的ID: FileUpload_Pic int imgdatalen = FileUpload_Pic。
PostedFile。ContentLength;//获取上传文件的大小 //文件名 string FileName = FileUpload_Pic。PostedFile。FileName。Substring(FileUpload_Pic。
PostedFile。FileName。LastIndexOf("\\") + 1); //文件类型 string FileType = FileName。Substring(FileName。IndexOf('。
') + 1);//获取文件的后缀名,这里,可以根据文件的后缀名来判断该文件是否符合你的需求。 string imgtype = FileUpload_Pic。PostedFile。ContentType;//文件类型 string imgtitle = TextBox_PicID。
Text;//手工录入文件的ID if(FileType=="jpg"&&imgdatalen<100) { byte[] imgdata = new byte[imgdatalen]; int n = imgdatastream。
Read(imgdata, 0, imgdatalen); string connstr = "Data Source=。;Initial Catalog=KeepSecretSys;User ID=sa;Password=guogzliye8411!#"; SqlConnection connection = new SqlConnection(connstr); SqlCommand command = new SqlCommand("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)VALUES ( @imgtitle, @ )", connection); SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType。
VarChar, 50); paramTitle。Value = imgtitle; command。Parameters。Add(paramTitle); SqlParameter paramData = new SqlParameter("@imgdata", SqlDbType。
Image); paramData。Value = imgdata; command。Parameters。Add(paramData); SqlParameter paramType = new SqlParameter("@imgtype", SqlDbType。
VarChar, 50); paramType。Value = imgtype; command。Parameters。Add(paramType); connection。
Open(); int numRowsAffected = command。ExecuteNonQuery(); connection。Close(); }这段代码你需要稍微修改一下才可以用。
在判断文件是否为图片(根据后缀名判断)和文件是否超过100K。这里需要注意。 大概意思已经出来了。如果还有什么问题请留言。
最好把图片弄到文件夹里面去不要弄到数据库里面去啊
1 最好不同产品放在不同文件夹里面 if (!Directory.Exists(uploadfolder)){Directory.CreateDirectory(uploadfolder);2 判断文件格式 得到文件扩展名System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();3 判断文件大小 HtmlInputFile.PostedFile.ContentLength < 长度
答:" & PostData & "" '使用pre,原样输出格式 ' 借助RecordSet将二进制流转化成文本 Function BinaryToString...详情>>