I'm trying to download files through a popup window. Most of files download correctly, but some one through this error:
Error in /Controladores/Descarga.ashx?guid=4b5050f6-2f69-41e1-8bd6-1113686e9575 File D:...\FileName.pdf.
An error occurred while communicating with the remote host. The error code is 0x80070057.
at System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[] status, Byte[] header, Int32 keepConnected, Int32 totalBodySize, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32 doneWithSession, Int32 finalStatus, Boolean& async)
at System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean isFinal)
at System.Web.Hosting.ISAPIWorkerRequest.FlushResponse(Boolean finalFlush)
at System.Web.HttpResponse.Flush(Boolean finalFlush, Boolean async)
at System.Web.HttpResponse.Flush()
at DownloadFile.Send(HttpContext context) in D:...\AppCode\DownloadFile.vb:line 65
And this is the code in DownloadFile.vb:
Public Sub Send(context As HttpContext)
Dim fileData As Byte()
Dim bytesRead As Long
Using fs As New FileStream(PrivatePath, FileMode.Open, FileAccess.Read)
ReDim fileData(fs.Length)
bytesRead = fs.Read(fileData, 0, CInt(fs.Length))
fs.Close()
End Using
Try
With context.Response
If .IsClientConnected Then
.ClearContent()
.ClearHeaders()
.Buffer = True
.ContentType = "application/octet-stream"
.AddHeader("Content-Length", bytesRead.ToString())
.AddHeader("Content-Disposition", $"attachment;filename={New FileInfo(PrivatePath).Name}")
.BinaryWrite(fileData)
.Flush() 'This is line 65
.Close()
End If
End With
Catch ex As Exception
MailToAdmin($"Error ...", context.Request.RawUrl)
End Try
End Sub
Thank you.
$"attachment;filename=""{New FileInfo(PrivatePath).Name}""". Doubled double-quotes make one literal double-quote.