0

シンプルなものが欠けています。

GridView にバインドするバイナリ ファイルをいくつか生成しています。

    FileDownloadGrid.DataSource = downloadList;
    FileDownloadGrid.DataBind();

グリッドの興味深い部分は、次のようにコーディングされています。

    <asp:TemplateField>
       <ItemTemplate>
         <asp:LinkButton ID="DownloadFile" runat="server" Text="Download" CommandName="DownloadFile1"
           CommandArgument='<%#Eval("FullName") +"|" + Eval("Name") %>'> 
         </asp:LinkButton>
       </ItemTemplate>
     </asp:TemplateField>

IFRAME Ajax メソッドを実行してファイルをダウンロードしようとしています。

function InitializeRequest(sender, args) {
  // Check to be sure this async postback is actually
  //   requesting the file download.
  if (sender._postBackSettings.sourceElement.id == "FileDownloadGrid") {
    // Create an IFRAME.
    var iframe = document.createElement("iframe");

    // Get the desired region from the dropdown.
    var fName = $get("CommandArgument").value;

    // Point the IFRAME to GenerateFile, with the
    //   desired region as a querystring argument.
    iframe.src = "Default2.aspx?fName=" + fName;

    // This makes the IFRAME invisible to the user.
    iframe.style.display = "none";

    // Add the IFRAME to the page.  This will trigger
    //   a request to GenerateFile now.
    document.body.appendChild(iframe);
  }
}

Web で見つけたものから、CommandArgument クライアント側を取得できず、スクリプトで「FullName」を取得する方法がわかりません。

誰かが私を正しい方向に向けることができますか? 私はかなりシンプルであるべき何かのために髪を引っ張っています。

ありがとう

遺伝子

4

1 に答える 1

0

歯ぎしりの末、JavaScript 関数を直接呼び出すことにしました。

ここにJavaScriptがあります:

function DownloadFile(filename) {
    // Check to be sure this async postback is actually
    // Create an IFRAME.

    var iframe = document.createElement("iframe");
    // Point the IFRAME to GenerateFile, with the
    //   desired region as a querystring argument.
    iframe.src = "Default2.aspx?fileName=" + filename;

    // This makes the IFRAME invisible to the user.
    iframe.style.display = "none";

    // Add the IFRAME to the page.  This will trigger
    //   a request to GenerateFile now.
    document.body.appendChild(iframe);
}

これが私のために働いたコードです:

<asp:LinkButton ID="DownloadFile" runat="server" Text="Download"  
   onClientClick='<%# string.Format("DownloadFile(\"{0}\");", Eval("FullName")) %>'></asp:LinkButton>

重要な部分は、'FullName' パスを \ から / に変換しているようです。

      string serverPath = toFileName.Replace("\\", "/");

次に、default2.aspx.cs でこれを行います。

protected void Page_Load(object sender, EventArgs e)
{
  string workPath = Request.QueryString["fileName"];
  string fullPath = workPath.Replace('/', '\\');
  string fileName = Path.GetFileName(fullPath);
  string attachmentHeader = String.Format("attachment; filename={0}", fileName);
  Response.AppendHeader("content-disposition", attachmentHeader);
  Response.ContentType = "application/octet-stream";
  Response.WriteFile(fullPath);
  Response.End();
}

これらすべてを行うためのより良い方法があると確信していますが、これは私がハッキングして思いついたものであり、他の人の助けになることを願っています.

于 2013-06-12T02:43:04.700 に答える