asp.net上传图片并同时生成缩略图
点击次数:21 次 发布日期:2008-11-26 15:55:48 作者:源代码网
|
源代码网推荐 Sub UploadFile(sender As Object, e As EventArgs) 源代码网推荐 源代码网推荐 If FileUp.PostedFile.ContentLength = 0 Then 源代码网推荐 FileInfo.Visible = False 源代码网推荐 Exit Sub 源代码网推荐 Else 源代码网推荐 FileInfo.Visible = True 源代码网推荐 FDisplay1.Visible = True 源代码网推荐 End If 源代码网推荐 源代码网推荐 FSize.Text ="上传文件大小"+ CStr(FileUp.PostedFile.ContentLength/1024)+"KB" 源代码网推荐 FName.Text = "已上传文件名:"+FileUp.PostedFile.FileName+"<br>"+FName.Text 源代码网推荐 源代码网推荐 "写入数据库 源代码网推荐 on error resume next 源代码网推荐 dim myconn as sqlconnection 源代码网推荐 dim mycomm as sqlcommand 源代码网推荐 dim sql as string 源代码网推荐 dim id as integer 源代码网推荐 Dim image,anewimage As System.Drawing.Image 源代码网推荐 dim width,height,newwidth,newheight as integer 源代码网推荐 Dim callb As System.Drawing.Image.GetThumbnailImageAbort 源代码网推荐 myConn=New sqlconnection(ConfigurationSettings.AppSettings("数据库")) 源代码网推荐 myconn.open() 源代码网推荐 sql="insert into picture (姓名,班级,介绍,属性) values (""&request.cookies("dgxyl").values("dgxylname")&"",""&request.cookies("dgxyl").values("dgxylbj")&"",""&trim(request("TextBox1"))&"",""&request("r1")&"")" 源代码网推荐 Mycomm=New sqlcommand(sql,myconn) 源代码网推荐 mycomm.executenonquery() 源代码网推荐 myconn.close() 源代码网推荐 源代码网推荐 Dim myCommand As New SqlCommand("select top 1 id from picture order by id desc", myConn) 源代码网推荐 myCommand.Connection.Open() 源代码网推荐 Dim myReader As SqlDataReader=mycommand.executereader() 源代码网推荐 if myReader.Read() then 源代码网推荐 id=myReader("id") 源代码网推荐 end if 源代码网推荐 myconn.close() 源代码网推荐 "保存图片 源代码网推荐 FileUp.PostedFile.SaveAs( Server.MapPath("classpic")&cstr(id)&".jpg" ) 源代码网推荐 "生成缩略图 源代码网推荐 image=System.Drawing.Image.FromFile(Server.MapPath("/classpic/"+cstr(id)+".jpg")) 源代码网推荐 width=image.Width 源代码网推荐 height=image.height 源代码网推荐 if width>height then 源代码网推荐 newwidth=250 源代码网推荐 newheight=image.height/image.Width*newwidth 源代码网推荐 else 源代码网推荐 newheight=250 源代码网推荐 newwidth=image.Width/image.height*newheight 源代码网推荐 end if 源代码网推荐 response.write("id="+cstr(id)+"width="+cstr(Width)+";height="+cstr(height)+" ") 源代码网推荐 response.write("newwidth="+cstr(newwidth)+";newheight="+cstr(newheight)+"<br>") 源代码网推荐 源代码网推荐 aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr()) 源代码网推荐 aNewImage.Save(Server.MapPath("/smallpic/"+cstr(id)+".jpg")) 源代码网推荐 image.Dispose() 源代码网推荐 源代码网推荐 源代码网推荐 Dim FileSplit() As String = Split( FileUp.PostedFile.FileName, "" ) 源代码网推荐 Dim FileName As String = FileSplit(FileSplit.Length-1) 源代码网推荐 Dim Exts() As String = Split( FileName, "." ) 源代码网推荐 Dim Ext As String = LCase(Exts(Exts.Length-1)) 源代码网推荐 源代码网推荐 FDisplay.Text = "<A Target="_blank" HREF="/classpic/"&cstr(id)&".jpg"& "">查看上传文件</A>" 源代码网推荐 FDisplay1.text="<a href="/picture/default.asp?bj="&cstr(request.cookies("dgxyl").values("dgxylbj"))&"">返回</a>" 源代码网推荐 End Sub 源代码网推荐 </script> 源代码网推荐 <SCRIPT language=JavaScript> 源代码网推荐 <!-- 源代码网推荐 var requestsubmitted=false; 源代码网推荐 function guestbook_Validator(theForm) 源代码网推荐 { 源代码网推荐 //检查是否从新提交 源代码网推荐 if (requestsubmitted==true){ 源代码网推荐 alert("你已经提交了留言,请等待服务器应答!"); 源代码网推荐 return(false); 源代码网推荐 } 源代码网推荐 requestsubmitted=true; 源代码网推荐 源代码网推荐 return (true); 源代码网推荐 } 源代码网推荐 //--> 源代码网推荐 </SCRIPT> 源代码网推荐 <Html> 源代码网推荐 <Body BgColor=White> 源代码网推荐 <H3 align="center">请正确填写下面各项</h3> 源代码网推荐 <Hr></H3> 源代码网推荐 <Div id="FileInfo" Visible="False" runat="server"> 源代码网推荐 <Asp:Label id="FSize" runat="server"/><br> 源代码网推荐 <Asp:Label id="FName" runat="server"/><br> 源代码网推荐 <Asp:Label id="FDisplay" runat="server"/> 源代码网推荐 <Asp:Label id="FDisplay1" runat="server"/> 源代码网推荐 </Div> 源代码网推荐 <Form Enctype="multipart/form-data" onsubmit="return guestbook_Validator(this)" runat="server"> 源代码网推荐 上传文件 源代码网推荐 <Input Type="File" id="FileUp" runat="server" size="20"><br> 源代码网推荐 图片属性:<input type="radio" value="<%=request.cookies("dgxyl").values("dgxylbj")%>" name="R1" checked>本班<input type="radio" value="全校" name="R1">全校(本班则只在本班显示,全校则在全校显示)<P> 源代码网推荐 图片说明:<br> 源代码网推荐 <asp:TextBox id="TextBox1" runat="server" Width="233px" Height="141px"> 源代码网推荐 </asp:TextBox> 源代码网推荐 <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="请输入图片说明"> 源代码网推荐 </asp:RequiredFieldValidator><br> 源代码网推荐 <Asp:button id="Upload" OnClick="UploadFile" Text="上传图片" runat="server"/> 源代码网推荐 </form> 源代码网推荐 <Hr> 源代码网推荐 <p>注意:</p> 源代码网推荐 <ol> 源代码网推荐 <li><b>严禁上传污染环境的照片,否则账号将被删除!</b></li> 源代码网推荐 <li><font color="#808000"><b>请详细填写照片说明,图片说明不详细将会被视为乱传图片,将会被删除!</b></font></li> 源代码网推荐 </ol> 源代码网推荐 <p> </p> 源代码网推荐 源代码网推荐 </Body> 源代码网推荐 </Html> 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
