locked
Issue with download the file in aspx RRS feed

  • Question

  • User-1454172184 posted

    In given code file is downloading successfully but all code not executing.

    protected void GridViewTicketResult_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                   var row = GridViewTicketResult.DataKeys[Convert.ToInt32(e.CommandArgument)].Values;
                   var path =row["url"].ToString()
                   bool isURL=true;
                    if (isURL)
                    {
                        DivCustomerSignature.Visible = true;
                        btnCustomerSignature.Text = "Sign as " + Convert.ToString(Session["ContactName"]);
                        CheckBox1.Checked = false;
                        hdnDocumentNumber.Value = DocumentNumber;
                        hdndb.Value = Db;
                        
                        string url = path;
    
                        WebClient webClient = new WebClient();
                        byte[] Content = webClient.DownloadData(url);
                        string fileName = (url.Split('/')[url.Split('/').Length - 1]).Split('.')[0];
    
                        Response.ContentType = "text/pdf";
                        Response.AddHeader("content-disposition", "attachment; filename=" + input.DocumentNumber + ".pdf");
                        Response.BufferOutput = true;
                        Response.OutputStream.Write(Content, 0, Content.Length);
                        Response.Flush();
                        //Response.End();
    
                        //byte[] bytes = webClient.DownloadData(url);
                        //string fileName = (url.Split('/')[url.Split('/').Length - 1]).Split('.')[0];
                        //Response.ContentType = "text/pdf";
                        //Response.AppendHeader("Content-Disposition", "attachment; filename=" + input.DocumentNumber + ".pdf");
                        //Response.BinaryWrite(bytes);
                        //Response.Flush();
                        //Response.SuppressContent = true;
    
                    }
    }

    This code not running. 

     DivCustomerSignature.Visible = true;
                        btnCustomerSignature.Text = "Sign as " + Convert.ToString(Session["ContactName"]);
                        CheckBox1.Checked = false;
                        hdnDocumentNumber.Value = DocumentNumber;
                        hdndb.Value = Db;

    I have been checked several solution on internet but did not find desired solution. Please provide needful solution.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/7969e3ea-7273-4529-bfb9-ca8f29d3e5aa/issue-with-download-the-file-in-aspx?forum=Offtopic#

    Thanks

    Monday, August 19, 2019 1:53 PM

All replies

  • User303363814 posted

    Is row["url"] throwing an exception?  Use the debugger to check the value of 'row'

    Maybe the first statement throws an exception.  Use the debugger to see what is happening.

    Basic steps

    1) Set a breakpoint on the first line

    2) Run the code

    3) Are the parameters 'sender' and 'e' what you expect?

    4) Single step

    5) Was an Exception thrown?  Fix it

    6) Does 'row' have the value you expect?

    7) Single step

    8) Is an exception thrown?  Then 'row' probably does not have the value you are expecting.  Fix it

    9) Does 'path' have the value you expect?

    Step, check, repeat

    Monday, August 19, 2019 11:04 PM
  • User-1716253493 posted

    Seem like you want to export and do some server side action for the controls at same time. AFAIK, you can't do that.

    You can do some trick, change the server controls first, use hiddenfield, viewstate, session or global variable to pass values and additional export flag.

    After postback, in page load get the flag, based the flag and values do export in page_load event, change the flag while export.

    Tuesday, August 20, 2019 12:53 AM
  • User665608656 posted

    Hi ankitkumar,

    Based on your description, I recommend that you use ajax to meet your needs.

    You can get the url of your current line in js, pass it to webmethod as a parameter, download it in webmethod, get the session value back to ajax, and change the corresponding control properties in js.

    Here is the js code:

     <script type="text/javascript">
        function BtNewClick(btview){
    var url=btview.attributes[2].value;
               $.ajax({
                    type: "POST",
                    url: "WebForm_0820_2158907.aspx/DownLoadPDf",
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                     data:"{ url: '"+url+"'}" ,
                    success: function (result) {
    //do something change of your control
    $("#Button1").attr("visible","true");
    $("#btnCustomerSignature").val(result.d);
                    }
                });
        }
        </script>
      <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="False" DataKeyNames="EmplId" DataSourceID="SqlDataSource1">
                    <Columns>
                        <asp:TemplateField HeaderText="Logged Ticket">
                            <ItemTemplate>
                                <asp:LinkButton ID='btview' runat="server" Text='download'
                                    CommandName="download" data-CommandArgument='<%#Eval("url") %>'
                                    OnClientClick="BtNewClick(this);return false;">
                                </asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="EmplId" HeaderText="EmplId" InsertVisible="False" ReadOnly="True" SortExpression="EmplId" />
                        <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                        <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                        <asp:BoundField DataField="url" HeaderText="url" SortExpression="url" />
                        <asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />
    
                    </Columns>
                </asp:GridView>

    .cs code:

      [WebMethod]
            public static string DownLoadPDf(string url)
            {
                 // do something what you need to download pdf
                string kk = "Sign as" + HttpContext.Current.Session["ContactName"].ToString();
                return kk;
            }

    Best Regards,

    YongQing.

    Tuesday, August 20, 2019 10:24 AM
  • User856008996 posted

    https://ulaskayalar.blogspot.com/2019/07/aspnet-c-uzak-sunucudanurl-ve-yerel.html

    Sunday, August 25, 2019 4:05 PM