I have a website that has large files uploaded and downloaded by members. There might be 30 or more simultaneous downloads going on at any one time. I am getting an error of the above subject.
My host gives me unlimited bandwidth .
I am including the piece of code that download the file to the user. IS there a way to optimize the code that I won't get these exceptions and users will be able to download files uninterrupted.
Is there any suggestion to either write a better code block or way to download the file where I won't get this exception and my users can download the files they want.
Thanks in advance
Dim bps As Long = 153600 ' kbps * 1024
Dim dir As New helper
_url = dir.GetDLUrl(pid, type)
Dim hlp As New helper
Dim _tempNameStr As String = _url.Replace("http://member.URL.com", "~")
Dim filePath As String = Server.MapPath(_tempNameStr.Replace("~", ""))
Dim NFNameStr As String
Dim tempstr As String = ""
If _tempNameStr.IndexOf("~/Downloads/File/") Then
tempstr = _tempNameStr.ToLower()
NFNameStr = tempstr.Replace("~/downloads/File/", "")
NFNameStr = Path.GetFileNameWithoutExtension(NFNameStr).Replace(" ", "_") + Path.GetExtension(NFNameStr)
Else
tempstr = _tempNameStr.ToLower()
NFNameStr = tempstr.Replace("~/downloads/File2/", "")
NFNameStr = Path.GetFileNameWithoutExtension(NFNameStr).Replace(" ", "_") + Path.GetExtension(NFNameStr)
End If
Dim myFile As New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim FileBytes As Byte() = New Byte(myFile.Length - 1) {}
myFile.Close()
Dim startBytes As Long = 0
Dim _EncodedData As String = HttpUtility.UrlEncode(NFNameStr, Encoding.UTF8) '+ lastUpdateTiemStamp
Context.Response.Clear()
Context.Response.Buffer = False
Context.Response.ContentType = "application/x-zip-compressed"
Context.Response.AddHeader("Accept-Ranges", "bytes")
Context.Response.AppendHeader("ETag", """" + _EncodedData + """")
Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + NFNameStr)
Context.Response.AddHeader("Content-Length", (FileBytes.Length - startBytes).ToString())
Context.Response.AddHeader("Connection", "Keep-Alive")
Context.Response.ContentEncoding = Encoding.UTF8
'Context.Response.OutputStream.Write(FileBytes, 0, FileBytes.Length)
Using sourceStream As New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize)
' Create throttled destination stream.
Dim destinationStream As New ThrottledStream(Response.OutputStream, bps)
Dim buffer As Byte() = New Byte(BufferSize - 1) {}
Dim readCount As Integer = sourceStream.Read(buffer, 0, BufferSize)
Response.Buffer = False
While readCount > 0
destinationStream.Write(buffer, 0, readCount)
readCount = sourceStream.Read(buffer, 0, BufferSize)
End While
Dim dlRS As New PService
dlRS._UpdatePostingDL(pid)
sourceStream.Close()
End Using
CloseWindow()