[英]Downloading image from SQL Server on an ASP.NET C# page
我有一个fileupload控件,可将图像上载到SQL Server 2008数据库,并且我想使用一个保存对话框弹出框来下载它。 下载文件的代码执行得很好,但是当我单击下载链接按钮时没有任何反应。 该事件从后面的代码触发。 我唯一的猜测是再次导致此问题的是模式弹出窗口。
<Modalpopup>
</ModalPopup>
<UpdatePanel>
--DetailsView
--------LinkButton(Download)
--DetailsView
</UpdatePanel>
是页面的结构。
<!-- Details View here -->
<asp:DetailsView ID="dvReviewCases" runat="server" Height="50px"
AutoGenerateRows="False" CssClass="dvCSS"
OnDataBound="dvADReviewCases_DataBound"
onpageindexchanging="dvReviewCases_PageIndexChanging" onitemcommand="dvReviewCases_ItemCommand"
>
<Fields>
<asp:TemplateField HeaderStyle-CssClass="dvHeaderStyle" HeaderText="Evidence" ItemStyle-CssClass="dvValueField" >
<ItemTemplate>
<asp:LinkButton ID="btnDownload" runat="server" Text='<%#Eval("evidenceID") %>' CommandName="Download" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
背后的代码
// Download file from Evidence table
protected void dvReviewCases_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
String strCaseID = Session["caseID"].ToString();
String strEvidenceID = Session["evidenceID"].ToString();
//Search for evidence file
DbCommand cmd = GetData.CreateCommand("ADGetEvidence");
GetData.AddParameter(cmd, strEvidenceID, "@evidenceID");
GetData.AddParameter(cmd, strCaseID, "@caseID");
GetData.GetResults(cmd);
// Check if datatable has row returned
if (GetData.getTable().Rows.Count > 0)
{
String fileName = "Evidence" + Session["caseID"].ToString();
byte[] file = null;
String fileType = GetData.getTable().Rows[0][1].ToString();
// Typecast resulting row to byte
file = ((byte[])GetData.getTable().Rows[0][0]);
Response.ContentType = fileType;
// Give user option to download file
Response.AddHeader("Content-Disposition","attachment;filename=" + fileName);
Response.BinaryWrite(file);
Response.End();
}
}
}
为什么不尝试使用查询字符串。 这是我在查询字符串中使用的代码,无需从本地文件夹中创建所需图像的任何状态即可从数据库中获取图像。 希望对您有所帮助。
<%@ WebHandler Language="C#" Class="DisplayImg" %>
using System;
using System.Web;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public class DisplayImg : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string theID;
if (context.Request.QueryString["id"] != null)
theID = context.Request.QueryString["id"].ToString();
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = DisplayImage(theID);
byte[] buffer = new byte[2048];
int byteSeq = strm.Read(buffer, 0, 2048);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 2048);
}
}
public Stream DisplayImage(string theID)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SERVER"].ConnectionString.ToString());
string sql = "SELECT Server_image_icon FROM tbl_ServerMaster WHERE server_Code = @ID";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", theID);
connection.Open();
object theImg = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])theImg);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
只需在CS代码中添加一行
UploadImg.ImageUrl = "~/DisplayImg.ashx?id=" + code;
UpdatePanel内部的PostBack只能响应整个页面,因为MSAjax魔术将旧页面(在浏览器中)与PostBack生成的差异合并以呈现新版本。
使用所有必需的参数将LinkButton转换为HyperLink,并实现HttpHandler来提供二进制数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.