2017-10-05 20 views
0

リンクボタン(ダウンロード)をクリックしてアップロードしたファイルをダウンロードし、アップロードされたファイルをダウンロードする別のボタンをクリックすると、コードが表示されます。他のボタンをクリックしたときにファイルをダウンロードする

lblFile.Text = Issue.FileName; 

     if (lblFile.Text != "") 
     { 
      trAttachedFile.Visible = true; 
      lbtnDownload.PostBackUrl = "~/Download.aspx?file=" + lblFile.Text; 
     } 
     else 
     { 
      trAttachedFile.Visible = false; 
     } 

そして、ここでは

protected void Page_Load(object sender, EventArgs e) 
    { 

     if (!string.IsNullOrEmpty(Request.QueryString["file"])) 
     { 
      DownloadID = Request.QueryString["file"]; 
      if (StartDownload() == true) 
      { 
       lblMessage.Text = "Your download should start shortly"; 
      } 
      else 
      { 
       lblMessage.Text = "Download File does not exist"; 
      } 
     } 

private bool StartDownload() 
    { 
     if (DownloadID != "") 
     { 
      string downloadPath = WebConfigurationManager.AppSettings["SubPic"].ToString() + DownloadID; 
      FileInfo downloadFile = new FileInfo(downloadPath); 

      if (downloadFile.Exists) 
      { 
       Response.Clear(); 
       Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadFile.Name); 
       Response.AddHeader("Content-Length", downloadFile.Length.ToString()); 
       Response.ContentType = "application/octet-stream"; 
       Response.WriteFile(downloadFile.FullName); 
       Response.End(); 
       return true; 
      } 
     } 
     return false; 
    } 

背後Download.aspxコードだと、ここであなたはボタンのPostBackUrlプロパティを使用する場合、それはフォームのPostBackUrl

を変更するためのLinkBut​​ton

<tr runat="server" id="trAttachedFile"> 
      <td> 
       <asp:Label runat="server" Text="File Attachment:" /> 
      </td> 
      <td colspan="2"> 
       <asp:Label runat="server" ID="lblFile" /> 
       &nbsp; 
       <asp:LinkButton runat="server" Text="Download" ID="lbtnDownload" CssClass="lbtnDownload" /> 
      </td> 
      <td>&nbsp;</td> 
     </tr> 

答えて

0

ですPostBackUrlを元のコードに戻すコードを追加するかeあなたのケースでは、実際にポストバックURLを変更する必要はありません。ダウンロードURLに移動してダウンロードを開始してください。

+0

ありがとうございます。どうもありがとうございます –

関連する問題