下载方法将读取位于lnkReportFile.NavigateUrl元素中的CSV文件,并带上“保存”窗口以选择目录。
问题是它将所有数据读取到最后。但此外它还会读取当前的ASPX页面。因此,当我保存.CSV文件并打开它时,我可以看到文件末尾的html。有人可以帮忙解决这个问题吗?
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
答案 0 :(得分:1)