利用FileUpload來檢查檔案的MD5 CheckSum
這個範例主要是用來檢查檔案的MD5 CheckSum
透過CheckSum值可以來判斷檔案是否被修改過
Md5checksum.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Md5checksum.aspx.cs" Inherits="Md5checksum" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>未命名頁面</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:FileUpload ID="FileUpload1" runat="server" />
12 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="checksum" />
13 <br />
14 <asp:Label ID="Label1" runat="server"></asp:Label></div>
15 </form>
16 </body>
17 </html>
18
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>未命名頁面</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:FileUpload ID="FileUpload1" runat="server" />
12 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="checksum" />
13 <br />
14 <asp:Label ID="Label1" runat="server"></asp:Label></div>
15 </form>
16 </body>
17 </html>
18
Md5checksum.aspx.cs
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Security.Cryptography;
12 using System.Text;
13 using System.IO;
14
15 public partial class Md5checksum : System.Web.UI.Page
16 {
17 protected void Page_Load(object sender, EventArgs e)
18 {
19
20 }
21
22 protected string MD5checksum(byte[] buffer)
23 {
24 MD5 md5 = MD5.Create();
25 StringBuilder sb = new StringBuilder();
26
27 foreach (byte b in md5.ComputeHash(buffer))
28 {
29 sb.Append(b.ToString("x2").ToLower());
30 }
31
32 return string.Format("MD5 checksum:<br /> {0}", sb.ToString());
33
34 }
35 protected void Button1_Click(object sender, EventArgs e)
36 {
37 if (this.FileUpload1.HasFile)
38 {
39 this.Label1.Text = MD5checksum(this.FileUpload1.FileBytes);
40 }
41 }
42 }
43
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Security.Cryptography;
12 using System.Text;
13 using System.IO;
14
15 public partial class Md5checksum : System.Web.UI.Page
16 {
17 protected void Page_Load(object sender, EventArgs e)
18 {
19
20 }
21
22 protected string MD5checksum(byte[] buffer)
23 {
24 MD5 md5 = MD5.Create();
25 StringBuilder sb = new StringBuilder();
26
27 foreach (byte b in md5.ComputeHash(buffer))
28 {
29 sb.Append(b.ToString("x2").ToLower());
30 }
31
32 return string.Format("MD5 checksum:<br /> {0}", sb.ToString());
33
34 }
35 protected void Button1_Click(object sender, EventArgs e)
36 {
37 if (this.FileUpload1.HasFile)
38 {
39 this.Label1.Text = MD5checksum(this.FileUpload1.FileBytes);
40 }
41 }
42 }
43