locked
network error saving excel file to client computer RRS feed

  • Question

  • User2068566658 posted

    I am using the code above and it works great when I am testing, placing the PastureCnt.xls in my downloads folder.   However, when I publish on the server,  and run in Chrome from the cloud server, it shows me "Pasture.cnt below with the message "Failed.Network error"    I have read numerous instructions that indicate it should prompt me to download.    What am I missing?   Is there a way to use MapPath to go to my client machine?   I would be happy to have it placed in C:\Data folder.   

     Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
    
            Dim sValue As String = [String].Empty
            Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable).Copy()
            Dim HTMLContent As New StringBuilder()
            sValue = vbTab
            For Each dcolumn As DataColumn In dt.Columns
                HTMLContent.Append(dcolumn.ColumnName + sValue)
                sValue = vbTab
            Next
            HTMLContent.Append(vbLf)
            Dim cnt As Integer
            For Each dRow As DataRow In dt.Rows
                sValue = ""
                For cnt = 0 To dt.Columns.Count - 1
                    HTMLContent.Append(sValue & dRow(cnt).ToString())
                    sValue = vbTab
                Next
                HTMLContent.Append(vbLf)
            Next
            HttpContext.Current.Response.Clear()
            HttpContext.Current.Response.ClearContent()
            HttpContext.Current.Response.ClearHeaders()
            HttpContext.Current.Response.Charset = ""
            HttpContext.Current.Response.Buffer = True
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=PastureCnt.xls")
            HttpContext.Current.Response.Write(HTMLContent)
            HttpContext.Current.Response.Flush()
            HttpContext.Current.Response.Close()
            lblMessage.Visible = True
        End Sub

     

    Tuesday, February 7, 2017 1:24 AM

All replies

  • User-1509636757 posted

    Can you paste detailed error or screenshot for Failed.Network error?

    Is there a way to use MapPath to go to my client machine?   I would be happy to have it placed in C:\Data folder.   

    This is not possible in web applications as far as I know (It would be disastrous if web applications get such access to users' computer thru browser :) ).

    Tuesday, February 7, 2017 3:51 AM
  • User2068566658 posted

    For some reason, I can't download an image.    At the bottom of the Chrome Screen is the download tab and it shows the excel symbol with the message PastureCnt.xls  Failed - Network Error.    If I press "Show all" on the right it shows 

    PastureCnt.xls  Failed - Network error

    http://www.inv.ranches.org.strawberry.arvixe.com/Choice/PastureCnt.aspx

    Tuesday, February 7, 2017 8:16 PM
  • User-271186128 posted

    Hi jjobcorp,

    You could try to use the following code to download excel file:

    using (ExcelPackage p = new ExcelPackage())
    {
        //Code to fill Excel file with data.
    
    
        Byte[] bin = p.GetAsByteArray();
    
        Response.ClearHeaders();
        Response.ClearContent();
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", Nombre_Del_Libro + ".xlsx"));
        Response.BinaryWrite(bin);
        Response.Flush();
        Response.End();
    }   

    Best regards,
    Dillion

    Wednesday, February 15, 2017 8:36 AM
  • User2068566658 posted

    My program will write the excel file to my computer when I run it in test mode.   It puts it in my download folder.    When I publish to the web, I get the Failed - Network error.    How can I publish to the server on the web?    I would like to put it in the app_data folder.   If I can do that, I can then push it to my client machine.

    Sunday, March 5, 2017 7:51 PM
  • User140957712 posted
    HttpContext.Current.Response.AddHeader("Content-Length", HTMLContent.Length.ToString());

    You need to specify the content length also. Add the above line to fix the problem. 
    Friday, August 2, 2019 12:51 PM