Use HttpHandler can solve the problem. I made a link something like this:<a href="http://www.domain.com/downloadhandler.aspx?id=10>Download</a>downloadhandler.aspx is a httphandler here, it will get the correspondent file according to the id. In fact this is the approach to make a secure download for digital goods, it is very useful in e-Commence if you are selling digital good online.
to laochen(天行者) : can you share the source code of downloadhandler.aspx to us?
to laochen(天行者) : can you share the source code of downloadhandler.aspx to us?
to laochen(天行者) yes, i had trying this way just as it was,but what can we do if the id's value is the file such as .vb,.cs,.config,.resx. this problem just is my problem,not the way how to make a link
To All the friends here,The following is the HttpHandler I made for my freind's web site, it is working for both .zip and .exe files. I did not try on .vb, .cs, .config and .resx, but I believe it will work also as I feed file as "application/octet-stream". The code is in VB.NET, but I believe u guys should not be any problem on understanding.In order to use my code, u need modify your web.config accordingly.<configuration> <system.web> ...... <httpHandlers> <add verb="*" path="download.aspx" type="TheBooksWeb.DownloadHandler,TheBooksWeb"/> </httpHandlers> ....... </system.web> ........ </configuration>There is no need to construct a "download.aspx" file in this case.This is a sample download link I create, I send this link to my client in email.<A href="http://mydomain.com/utility/download.aspx?productid=10&token=3D2b12e304-3872-458e-a824-866677b1ccf8">Self-Made Millionaires</A>Imports System.Web Imports System.IO Imports System.Data.OleDbPublic Class DownloadHandler Implements IHttpHandler Private _strConn As String ' Override the ProcessRequest method. Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest Dim fPath As String = ConfigurationSettings.AppSettings("fPath") Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & fPath & "\db.mdb" Dim ProductId As String = context.Request.Params("productid") Dim Token As String = context.Request.Params("token") ' Create Instance of Connection and Command Object Dim myConnection As OleDbConnection = New OleDbConnection(strConn) Dim strSQL As String = "SELECT tokens.token, tokens.expire, tokens.productid, products.filename, categories.category " & _ "FROM (tokens INNER JOIN products ON tokens.productid = products.id) INNER JOIN categories ON products.categoryid = categories.id " & _ "WHERE productid=" & ProductId & " AND token='" & Token & "'" Try Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, myConnection) myConnection.Open() Dim result As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) If result.Read Then If result.Item("productid") = ProductId And result.Item("token") = Token Then Dim Expire As DateTime = result.Item("expire") Dim fLength As Long Dim Filename As String If DateTime.Compare(Expire, Now()) >= 0 Then Filename = fPath & "\books\" & result.Item("category") & "\" & result.Item("filename")
Dim fi As FileInfo = New FileInfo(Filename) fLength = fi.Length context.Response.ContentType = "application/octet-stream" context.Response.AppendHeader("Content-Disposition", "attachment; filename=" & System.IO.Path.GetFileName(Filename)) context.Response.AddHeader("Content-Length", fLength.ToString()) context.Response.WriteFile(Filename) Else Throw New Exception("Your link had been expired.") End If Else Throw New Exception("Either productid or token is invalid.") End If Else Throw New Exception("Either productid or token is invalid.") End If Catch ex As Exception context.Response.ContentType = "Text/HTML" context.Response.Write("<H1>Invalid Download Link</H1>") context.Response.Write("<H3>ProductId: " & ProductId & "</H3>") context.Response.Write("<H3>Token: " & Token & "</H3>") context.Response.Write("<H3>Reason:" & ex.Message & "</H3>") Return Finally myConnection.Close() End Try End Sub ' Override the IsReusable property. Public ReadOnly Property IsReusable() As Boolean _ Implements IHttpHandler.IsReusable Get Return True End Get End PropertyEnd Class
Continue:My file downloader is quite helpful, u can modify it accordingly and apply your own function, for instance, you can craete a download counter in your database before feeding files to the user.Cheers
To ddmor(ddmor),I confirm my method works as I did test on ".vb" extension name. You may try the link below. It will feed you a file, namely as "test.vb".http://202.126.180.73\utility\download.aspx?productid=11&token=supertoken
http://expert.csdn.net/Expert/topic/2264/2264082.xml?temp=9.945095E-03
System.IO.FileInfo myFileInfo = new System.IO.FileInfo(sfileFullName.ToString());
Response.Buffer=true;
Response.Clear();
Response.ContentType = "application/vnd.ms-word";
Response.AddHeader("Content-Disposition","attachment; filename="+HttpUtility.UrlEncode(sfileName,System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length",myFileInfo.Length.ToString());
Response.WriteFile(sfileFullName.ToString());
Response.End();
试了你那种方法还是不行呀。
==guoyan19811021(吉祥),asam2183(三山),qimini(循序渐进)
关于你们对iis设置的解释我一直都很迷茫,能不能具体的谈一下呢。
你的方法可行!!,不过遇到象.vb,.config,.resx,.cs等VisualStudio文件就会出现提示如下:
无法提供此类型的页。
说明: 由于已明确禁止所请求的页类型,无法对该类型的页提供服务。扩展名“.vb”可能不正确。 请检查以下的 URL 并确保其拼写正确。
好像是禁止下栽的
求教iis设置
can you share the source code of downloadhandler.aspx to us?
can you share the source code of downloadhandler.aspx to us?
yes, i had trying this way just as it was,but what can we do if the id's value is the file such as .vb,.cs,.config,.resx. this problem just is my problem,not the way how to make a link
<system.web>
...... <httpHandlers>
<add verb="*" path="download.aspx" type="TheBooksWeb.DownloadHandler,TheBooksWeb"/>
</httpHandlers> ....... </system.web>
........
</configuration>There is no need to construct a "download.aspx" file in this case.This is a sample download link I create, I send this link to my client in email.<A href="http://mydomain.com/utility/download.aspx?productid=10&token=3D2b12e304-3872-458e-a824-866677b1ccf8">Self-Made Millionaires</A>Imports System.Web
Imports System.IO
Imports System.Data.OleDbPublic Class DownloadHandler
Implements IHttpHandler Private _strConn As String ' Override the ProcessRequest method.
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim fPath As String = ConfigurationSettings.AppSettings("fPath")
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & fPath & "\db.mdb"
Dim ProductId As String = context.Request.Params("productid")
Dim Token As String = context.Request.Params("token") ' Create Instance of Connection and Command Object
Dim myConnection As OleDbConnection = New OleDbConnection(strConn)
Dim strSQL As String = "SELECT tokens.token, tokens.expire, tokens.productid, products.filename, categories.category " & _
"FROM (tokens INNER JOIN products ON tokens.productid = products.id) INNER JOIN categories ON products.categoryid = categories.id " & _
"WHERE productid=" & ProductId & " AND token='" & Token & "'" Try
Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, myConnection)
myConnection.Open()
Dim result As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
If result.Read Then
If result.Item("productid") = ProductId And result.Item("token") = Token Then
Dim Expire As DateTime = result.Item("expire")
Dim fLength As Long
Dim Filename As String If DateTime.Compare(Expire, Now()) >= 0 Then
Filename = fPath & "\books\" & result.Item("category") & "\" & result.Item("filename")
Dim fi As FileInfo = New FileInfo(Filename)
fLength = fi.Length context.Response.ContentType = "application/octet-stream"
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" & System.IO.Path.GetFileName(Filename))
context.Response.AddHeader("Content-Length", fLength.ToString())
context.Response.WriteFile(Filename)
Else
Throw New Exception("Your link had been expired.")
End If
Else
Throw New Exception("Either productid or token is invalid.")
End If
Else
Throw New Exception("Either productid or token is invalid.")
End If
Catch ex As Exception
context.Response.ContentType = "Text/HTML"
context.Response.Write("<H1>Invalid Download Link</H1>")
context.Response.Write("<H3>ProductId: " & ProductId & "</H3>")
context.Response.Write("<H3>Token: " & Token & "</H3>")
context.Response.Write("<H3>Reason:" & ex.Message & "</H3>")
Return
Finally
myConnection.Close()
End Try
End Sub ' Override the IsReusable property.
Public ReadOnly Property IsReusable() As Boolean _
Implements IHttpHandler.IsReusable
Get
Return True
End Get
End PropertyEnd Class