0

画像を 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();

            }

        }
    }
4

2 に答える 2

1

query string を使用してみませんか。これは私のコードで、クエリ文字列が使用され、ローカルフォルダーに目的の画像のインスタンスを作成することなく、データベースから画像をフェッチします。これが役に立ちます。

<%@ 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 コードに 1 行追加するだけです

UploadImg.ImageUrl = "~/DisplayImg.ashx?id=" + code;
于 2012-11-27T04:44:14.210 に答える
0

UpdatePanel内からのPostBackは、ページ全体でのみ応答できます。これは、MSAjaxマジックが古いページ(ブラウザー内)とPostBackによって生成された差分をマージして新しいバージョンをレンダリングするためです。

LinkBut​​tonを必要なすべてのパラメーターを使用してHyperLinkに変換し、バイナリデータを提供するためにHttpHandlerを実装します。

于 2012-11-26T21:00:24.600 に答える