Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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()
Posted

1 solution

Can't you just give them a link, and let them right-click/download them that way?
 
Share this answer
 
Comments
Member 7790032 14-Apr-11 14:58pm    
Your saying give them a direct link to the file. There is some house keeping that needs to be done. like the is a different download rate for various level of user membership. We need to know how many times a file is downloaded . SO I don't know if I can do the first one by giving the user a direct link or an absolute URL like <pre>http://www.myurl.com/downloads/file/file.zip</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900