今天討論區有人問到這樣的需求..
小弟就做一個範例實做這樣的功能..
首先要準備的東西如下:
DB,結構如圖(table name:fileupload)

file,
FileuploadGuid.aspx
FileuploadGuid.aspx.cs
filedownload.ashx
file(目錄)→存放檔案用
FileuploadGuid.aspx
01 |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileuploadGuid.aspx.cs" Inherits="FileuploadGuid" %> |
06 |
<title>FileuploadGuid</title> |
09 |
<form id="form1" runat="server"> |
11 |
<asp:FileUpload ID="FileUpload1" runat="server" /> |
12 |
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /> |
13 |
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" |
14 |
DataSourceID="SqlDataSource1"> |
16 |
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" |
17 |
SortExpression="id" /> |
18 |
<asp:BoundField DataField="guid" HeaderText="guid" SortExpression="guid" /> |
19 |
<asp:BoundField DataField="filename" HeaderText="filename" SortExpression="filename" /> |
20 |
<asp:TemplateField HeaderText="download"> |
22 |
<asp:HyperLink ID="HyperLink1" NavigateUrl='<%# "filedownload.ashx?guid="+Eval("guid") %>' |
23 |
runat="server">click</asp:HyperLink> |
28 |
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" |
29 |
SelectCommand="SELECT * FROM [fileupload]"></asp:SqlDataSource> |
FileuploadGuid.aspx.cs
03 |
using System.Configuration; |
04 |
using System.Collections; |
06 |
using System.Web.Security; |
08 |
using System.Web.UI.WebControls; |
09 |
using System.Web.UI.WebControls.WebParts; |
10 |
using System.Web.UI.HtmlControls; |
11 |
using System.Data.SqlClient; |
14 |
public partial class FileuploadGuid : System.Web.UI.Page |
16 |
protected void Page_Load(object sender, EventArgs e) |
20 |
protected void btnUpload_Click(object sender, EventArgs e) |
22 |
if (FileUpload1.HasFile) |
24 |
string guid = Guid.NewGuid().ToString(); |
25 |
string filename = this.FileUpload1.FileName; |
28 |
this.FileUpload1.SaveAs(Server.MapPath(string.Format(@"file\{0}.{1}", guid, Path.GetExtension(filename)))); |
32 |
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) |
34 |
string sql = "insert into [fileupload] ([guid],[filename]) values(@guid,@filename)"; |
35 |
SqlCommand cmd = new SqlCommand(sql, conn); |
36 |
cmd.Parameters.Add("guid", SqlDbType.Char, 36).Value = guid; |
37 |
cmd.Parameters.Add("filename", SqlDbType.NVarChar).Value = filename; |
39 |
cmd.ExecuteNonQuery(); |
43 |
this.GridView1.DataBind(); |
filedownload.ashx
01 |
<%@ WebHandler Language="C#" Class="filedownload" %> |
05 |
using System.Data.SqlClient; |
08 |
using System.Configuration; |
10 |
public class filedownload : IHttpHandler |
13 |
public void ProcessRequest(HttpContext context) |
15 |
string guid = context.Request.QueryString["guid"] as string; |
17 |
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) |
19 |
string sql = "select * from [fileupload] where guid=@guid"; |
20 |
SqlCommand cmd = new SqlCommand(sql, conn); |
21 |
cmd.Parameters.Add("guid", SqlDbType.Char, 36).Value = guid; |
23 |
SqlDataReader dr = cmd.ExecuteReader(); |
26 |
string filename = dr["filename"].ToString(); |
28 |
context.Response.Buffer = true; |
29 |
context.Response.Clear(); |
30 |
context.Response.ContentType = "application/download"; |
31 |
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ";"); |
32 |
context.Response.BinaryWrite(File.ReadAllBytes(context.Server.MapPath(string.Format(@"file\{0}.{1}", guid, Path.GetExtension(filename))))); |
33 |
context.Response.Flush(); |
34 |
context.Response.End(); |
40 |
public bool IsReusable |
執行結果:

參考網址:
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20090212105722WRJ&fumcde=FUM20041006161839LRJ#BRD20090212112634RZ4