[英]File download from database not working
I've inherited this site and having issues with file downloads. 我继承了这个网站,并在文件下载方面遇到了问题。 Files such as .pdf, .doxs, .txt are saved in Sql as image. 诸如.pdf,.doxs,.txt之类的文件在Sql中保存为图像。 A datalist controls is used with a a href
controls to view it/downloaded it but it's not working. 数据列表控件与a href
控件一起使用以查看/下载它,但是它不起作用。 And I realize that a href
control is not a proper control to use but not sure what control to use. 而且我意识到a href
控件不是要使用的适当控件,但不确定要使用什么控件。
Here is the aspx markup: 这是aspx标记:
<asp:DataList ID="DataList2" runat="server" DataSourceID="SqlDataSource2" DataKeyField="fileId">
<ItemTemplate>
<a href='<%# "~/UserControls/FileFetch.ashx?fileId=" + Convert.ToString(Eval("fileId")) %>' target="_blank">
<%# Convert.ToString(Eval("fileName"))%></a>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
SelectCommand="SELECT [fileId], [fileName], [postedBy] FROM [FilesLibrary]">
</asp:SqlDataSource>
A helper file called FileFetch.ashx is used to get the file: 名为FileFetch.ashx的帮助文件用于获取文件:
<%@ WebHandler Language="C#" Class="FileFetch" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;
public class FileFetch : IHttpHandler
{
const int BUFFERSIZE = 1024;
public bool IsReusable
{
get
{
return true;
}
}
public void ProcessRequest(HttpContext context)
{
HttpResponse response = context.Response;
HttpRequest request = context.Request;
response.ContentType = "response.ContentType";
response.Cache.SetCacheability(HttpCacheability.Public);
response.BufferOutput = false;
writeSingleImage(Convert.ToInt32(request.QueryString["fileId"]), response.OutputStream);
response.End();
}
public void writeSingleImage(int fileId, Stream output)
{
string cxnstr = System.Configuration.ConfigurationManager.ConnectionStrings["ClubSiteDB"].ConnectionString;
SqlConnection connection = new SqlConnection(cxnstr);
string query;
query = "SELECT fileData FROM dbo.FilesLibrary where fileId=@fileId";
SqlCommand command = new SqlCommand(query, connection);
SqlParameter param0 = new SqlParameter("@fileId", SqlDbType.Int);
param0.Value = fileId;
command.Parameters.Add(param0);
connection.Open();
byte[] d = ((byte[])(command.ExecuteScalar()));
output.Write(d, 0, d.Length);
connection.Close();
}
} }
I'm going to take a stab at this and say you need to change your Anchor
tag to be the following: 我要对此进行说明,说您需要将Anchor
标签更改为以下内容:
<a href='<%# Page.ResolveUrl("~/UserControls/FileFetch.ashx?fileId=" + Convert.ToString(Eval("fileId"))) %>' target="_blank">
The MSDN link to the Page.ResolveUrl
method is here . 指向Page.ResolveUrl
方法的MSDN链接在此处 。 Page inherits from Control, hence why the link refers to Control.ResolveUrl. 页面继承自Control,因此为什么链接引用Control.ResolveUrl。
I've just seen that another alternative is to add runat="server"
to the Anchor
tag to turn it into a server control. 我刚刚看到另一种替代方法是将runat="server"
添加到Anchor
标记中,以将其转变为服务器控件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.