For some reason your other thread redirects here so my response is lost.
Your code uses the file path on the server as the file name. You want the actual file name. Use the Path.GetFileName() method to grab the file name from the path.
Response.AddHeader("Content-Disposition", "attachment; filename=" & Path.GetFileName(Fname))
Response.Close() can cause problems. The reference documentation recommends Response.End() or HttpContext.Current.ApplicationInstance.CompleteRequest()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment; filename=" & Path.GetFileName(Fname))
WriteFile(Fname)
Response.End()
I'm pretty sure you not using ascyn methods correctly but it does not matter because execution never reaches DeleteTemporaryExportFileAsync() due to the Response.Close(). Secondly, there is no logical reason to implement an async method because the code is synchronous. Just delete the file once the WriteFile completes.
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment; filename=" & Path.GetFileName(Fname))
WriteFile(Fname)
File.Delete(Fname)
Response.End()
If you really want to implement an async delete then replace Response.End() with HttpContext.Current.ApplicationInstance.CompleteRequest()
Lastly, I tested the WriteFile() method with a 350,000 KB using the updated code above and logic worked as expected. WriteFile() methods writes to the response stream in 300,000 byte blocks. Once the WriteFile() finished the file is deleted. Below is my test code.
Public Class _Default11
Inherits System.Web.UI.Page
Dim Fname As String = "C:\Temp\largefile.xls"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment; filename=" & Path.GetFileName(Fname))
WriteFile(Fname)
File.Delete(Fname)
Response.End()
End Sub
Private Sub WriteFile(ByVal Fname As String)
Dim MyFileInfo As FileInfo
Dim StartPos As Long = 0
Dim FileSize, ThisWrite, LeftToWrite As Long
MyFileInfo = New FileInfo(Fname)
FileSize = MyFileInfo.Length
LeftToWrite = FileSize
While LeftToWrite > 0
ThisWrite = IIf(LeftToWrite > 300000, 300000, LeftToWrite)
Response.WriteFile(Fname, StartPos, ThisWrite)
Response.Flush()
StartPos += ThisWrite
LeftToWrite -= ThisWrite
End While
End Sub
End Class
It would be very helpful if you can revert back to a code base that worked as expected. It's not clear if this bug is due to your updates or is an existing bug.