aspx网页代码
1
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
upload.aspx.cs
"
Inherits
=
"
upload
"
%>
2
3 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4
5 < html xmlns ="http://www.w3.org/1999/xhtml" >
6 < head runat ="server" >
7 < title > 无标题页 </ title >
8 < script language ="javascript" type ="text/javascript" >
9 function preview(imageurl)
10 {
11 document.getElementById( " Image1 " ).src = imageurl;
12 }
13 </ script >
14 </ head >
15 < body >
16 < form id ="form1" runat ="server" >
17 < div >
18
19 < asp:FileUpload ID ="FileUpload1" runat ="server" onchange ="preview(this.value);" />
20 < asp:Button ID ="Button1" runat ="server" onclick ="Button1_Click" Text ="上传" />
21 < br />
22 < asp:Image ID ="Image1" runat ="server" />
23
24 </ div >
25 </ form >
26 </ body >
27 </ html >
28
2
3 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4
5 < html xmlns ="http://www.w3.org/1999/xhtml" >
6 < head runat ="server" >
7 < title > 无标题页 </ title >
8 < script language ="javascript" type ="text/javascript" >
9 function preview(imageurl)
10 {
11 document.getElementById( " Image1 " ).src = imageurl;
12 }
13 </ script >
14 </ head >
15 < body >
16 < form id ="form1" runat ="server" >
17 < div >
18
19 < asp:FileUpload ID ="FileUpload1" runat ="server" onchange ="preview(this.value);" />
20 < asp:Button ID ="Button1" runat ="server" onclick ="Button1_Click" Text ="上传" />
21 < br />
22 < asp:Image ID ="Image1" runat ="server" />
23
24 </ div >
25 </ form >
26 </ body >
27 </ html >
28
cs文件代码
1
using
System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Linq;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13
14 public partial class upload : System.Web.UI.Page
15 {
16 protected void Page_Load( object sender, EventArgs e)
17 {
18
19 }
20 protected void Button1_Click( object sender, EventArgs e)
21 {
22 Random rad = new Random();
23 string pathname = Server.MapPath( " upload/ " );
24 string filename = FileUpload1.FileName;
25 string filesavename = DateTime.Now.Millisecond + rad.Next() + filename;
26 int filesize = FileUpload1.PostedFile.ContentLength;
27 string filetype = filename.Substring(filename.LastIndexOf( " . " ) + 1 );
28 if (filetype == " jpg " || filetype == " jpeg " || filetype == " gif " )
29 {
30 if (filesize < 200000 )
31 {
32 FileUpload1.PostedFile.SaveAs(Server.MapPath( " ~/upload/ " + filesavename));
33 // Image1.ImageUrl="~/upload/"+filesavename;
34 }
35 else
36 {
37 Response.Write( " <script>alert('图片太大!');</script> " );
38 }
39 }
40 else
41 {
42 Response.Write( " <script>alert('上传格式不正确!');</script> " );
43 }
44 }
45 }
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Linq;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13
14 public partial class upload : System.Web.UI.Page
15 {
16 protected void Page_Load( object sender, EventArgs e)
17 {
18
19 }
20 protected void Button1_Click( object sender, EventArgs e)
21 {
22 Random rad = new Random();
23 string pathname = Server.MapPath( " upload/ " );
24 string filename = FileUpload1.FileName;
25 string filesavename = DateTime.Now.Millisecond + rad.Next() + filename;
26 int filesize = FileUpload1.PostedFile.ContentLength;
27 string filetype = filename.Substring(filename.LastIndexOf( " . " ) + 1 );
28 if (filetype == " jpg " || filetype == " jpeg " || filetype == " gif " )
29 {
30 if (filesize < 200000 )
31 {
32 FileUpload1.PostedFile.SaveAs(Server.MapPath( " ~/upload/ " + filesavename));
33 // Image1.ImageUrl="~/upload/"+filesavename;
34 }
35 else
36 {
37 Response.Write( " <script>alert('图片太大!');</script> " );
38 }
39 }
40 else
41 {
42 Response.Write( " <script>alert('上传格式不正确!');</script> " );
43 }
44 }
45 }