kk-dev11
kk-dev11

Reputation: 2674

Error in Reading CSV Files

The Download Method will read the CSV-File, which is in the lnkReportFile.NavigateUrl Element, and bring a "Save" Window to choose the directory.

The problem is it reads all the data till the end. But then it additionally reads the current ASPX page as well. So when I save the .CSV File and open it, i can see the html at the end of the file. Could anyone please help solving this problem?

Private Sub btnCreateReport(....) Handles ..
    Try
        'Logic comes here..'

        'At the end
        ' Link setzen und anzeigen
        lnkReportFile.NavigateUrl = strPathToNavigate
        lnkReportFile.Visible = True

    Catch ex As Exception
    Finally
        ' Datei schliessen über den StreamWriter
        If Not writer Is Nothing Then ' es kann sein, das der StreamWriter nicht initialisiert wurde
            writer.Close()
            downloadReport()
        End If
    End Try
End Sub

Private Sub downloadReport()
        Dim fileName As String = System.IO.Path.GetFileName(lnkReportFile.NavigateUrl)
        Dim fileToDownload As String = Server.MapPath("~\" & Constants.Reports.strReportPath & "\" & fileName)

    Response.ContentType = "application/octet-stream"
    Dim cd As ContentDisposition = New ContentDisposition()
    cd.Inline = False
    cd.FileName = System.IO.Path.GetFileName(fileName)
    Response.AddHeader("Content-Disposition", cd.ToString())

    Dim fileData As Byte() = System.IO.File.ReadAllBytes(fileToDownload)
    Response.OutputStream.Write(fileData, 0, fileData.Length)
End Sub

Upvotes: 0

Views: 79

Answers (1)

kostas ch.
kostas ch.

Reputation: 2035

Try something the above :

Response.Flush();
Response.End();

I hope that helps.

This is "stolen" from here . It worked fine with ajax and asp.net an also with mvc.

Upvotes: 1

Related Questions