Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

large file/stream download problem

68 views
Skip to first unread message

Patrick Degenaar

unread,
Jun 25, 2002, 10:06:45 AM6/25/02
to
Hi,

I'm having problems downloading large files (1-2 Gb) to the client from my
ASP server. I'm using the Response.ContentType = "application/octet-stream"
to force downloads on the client and this works fine up until 100-195 Mb.
Files larger than this just cut out at some point between 100-195Mb
depending on the server (It seems to vary from computer to computer). The
progress bar remains on the client, but there is no more communication
between the machines. The ASP/C# code used on the various test servers is
identical, and the IIS settings are identical. I've set the Web.Config:

<httpRuntime
maxRequestLength="10000"
executionTimeout="3600"
/>

I've also set the IIS configuration/App Options timeouts to 1 hour, and the
network works fine.

Can anyone help? My C# code is below:

Thanks

Patrick

{
Stream theStream = // Stream from database //
string fileName = // fileName of stream//
string fileLength = theStream.Length.ToString();

int buffer = 65536;
BinaryReader br = new BinaryReader(theStream);

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename="+
fileName);
Response.AddHeader("Content-Length", fileLength);
Response.Flush();

while(br.PeekChar() > -1)
{
Response.BinaryWrite(br.ReadBytes(buffer));
Response.Flush();
br.BaseStream.Flush();
GC.Collect();
}
br.Close();
theStream.Close();
}


Eph0nk

unread,
Jun 25, 2002, 10:37:00 AM6/25/02
to
This is a known limitation. It depends on the amount of RAM in the server.
You should use components that send the file piece by piece, i don't think
it's included in the .net framework.
---
Tim De Vogel
S-Data NV

"Patrick Degenaar" <Patrick_...@nai.com> schreef in bericht
news:eoXV2FFHCHA.2640@tkmsftngp11...

Patrick Degenaar

unread,
Jun 25, 2002, 10:57:19 AM6/25/02
to
Hmmm,

Strange thing is, My laptop (128Mb) stalls after sending 194Mb, while my
Workstation (512Mb) stalls at 36.8 Mb, using exactly the same settings.

How do you suggest I send the file Piece by piece?

Thanks

Patrick

"Eph0nk" <tim.antispam...@s-data.be> wrote in message
news:3d188051$0$8144$ba62...@news.skynet.be...

Jim Ross [MVP]

unread,
Jun 25, 2002, 12:56:15 PM6/25/02
to
"Patrick Degenaar" <Patrick_...@nai.com> wrote:

>How do you suggest I send the file Piece by piece?

See if this helps. No guarantees, but I think it is what you want:

Handling Custom File Downloads in ASP.NET With Server Controls
[C# -BETA 2] By Peter A. Bromberg, Ph.D.
http://www.eggheadcafe.com/articles/20011006.asp

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<HTML>
<HEAD>
<script runat="server" ID=Script1>
void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack){
FileStream MyFileStream = new
FileStream(@"d:\inetpub\wwwroot\small.pdf", FileMode.Open);
long FileSize;
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
MyFileStream.Close();
Response.ContentType="application/pdf";
Response.AddHeader( "content-disposition","attachment;
filename=MyPDF.PDF");
Response.BinaryWrite(Buffer);
}
}
</script>
</HEAD>
<body>
<form runat="server" ID="Form1">
<asp:button id="link1" Text = "get PDF" runat="server" />
</form>
</body>
</HTML>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

READER UPDATE:

I received the following from a reader (webm...@lovespyt.com)
after this article was originally posted. I think it contains some
more valuable information on this subject, and I'm reprinting it with
the author's permission:

"You recently discussed custom file downloads at
http://www.eggheadcafe.com/articles/20011006.asp,
and I just wanted to send you some VB.NET code I created to do the
same
thing, but to also extend that by adding basic support for download
resume programs such as Go!Zilla and GetRight.

Support is not (yet) added for segmented downloading feature in
Getright,
because it looks like ASP.NET is tossing up some error, but
unfortunately
I don't know how to analyze GetRights request string or the response
of
the ASP.NET page to that request. It's not full W3C standards
compatible,
as the range header can include multiple segments
(http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16).

Also important in this resuming was to make sure that it sent a 206
message
back (Partial Content) as opposed to a 200 (OK) message. I found when
developing
this, it was important to set both the code and the description.

Anyways, this should help out developers looking to secure various
files without
having to map extensions to IIS and the like. I use this class to
serve dynamic
videos after first checking user priviledges and a download count."

Imports System
Imports System.Web
Imports System.IO

Namespace LovesPYT

Public Class FileHandling

Shared Public Sub DownloadFile(FilePath as String, Optional
ContentType as String = "")

If File.Exists(FilePath) Then
Dim myFileInfo as FileInfo
Dim StartPos as Long = 0, FileSize as Long, EndPos as Long

myFileInfo = New FileInfo(FilePath)
FileSize = myFileInfo.Length
EndPos = FileSize

HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ClearHeaders()
HttpContext.Current.Response.ClearContent()

Dim Range as String =
HttpContext.Current.Request.Headers("Range")
If Not ((Range Is Nothing) or (Range = "")) Then
Dim StartEnd as Array =
Range.SubString(Range.LastIndexOf("=")+1).Split("-")
If Not StartEnd(0)="" Then
StartPos = CType(StartEnd(0), Long)
End If

If StartEnd.GetUpperBound(0) >= 1 and Not StartEnd(1)=""
Then
EndPos = CType(StartEnd(1), Long)
Else
EndPos = FileSize-StartPos
End If

If EndPos > FileSize then
EndPos = FileSize - StartPos
End If

HttpContext.Current.Response.StatusCode=206
HttpContext.Current.Response.StatusDescription="Partial
Content"
HttpContext.Current.Response.AppendHeader("Content-Range",
"bytes " & StartPos &"-"& EndPos & "/" & FileSize)
End If

If Not (ContentType="") and (StartPos = 0) Then
HttpContext.Current.Response.ContentType = ContentType
End If


HttpContext.Current.Response.AppendHeader("Content-disposition",
"attachment; filename=" & myFileInfo.Name)
HttpContext.Current.Response.WriteFile(FilePath, StartPos,
EndPos)
HttpContext.Current.Response.End()
End If

End Sub
End Class
End Namespace


Peter Bromberg is a Senior Programmer /Analyst at FiServ, Inc. in
Orlando and a co-developer of the EggheadCafe.com developer website.
He can be reached at pbro...@yahoo.com

Jim Ross
MS MVP [ASP.NET][VC/MFC emeritus]

To send email, change 'lotsofspamthroughhere' to 'msn' but please ask all questions in the newsgroups, not via private mail

Patrick Degenaar

unread,
Jun 26, 2002, 5:01:25 AM6/26/02
to
Hmmm,

Thanks for that, but the given code writes everything into a buffer, which
is fine for small files, but becomes a memory problem for large files
(>100Mb). The files I need to send are CD and DVD images which are 600 -
5000 MB in size.

My own code was piece by piece as such, as it implemented a read - write
loop using buffer sizes of 65535 bytes. However it seems to stall, leaving
the download bar hanging but doing no further download.

Patrick

"Jim Ross [MVP]" <ro...@lotsofspamthroughhere.com> wrote in message
news:p38hhu0dhs6rqh0q5...@4ax.com...

Jim Ross [MVP]

unread,
Jun 26, 2002, 10:31:36 AM6/26/02
to
I haven't tried it, but the additional response at the end of the
article looks like it would handle chopping up the file.

Patrick Degenaar

unread,
Jun 27, 2002, 4:10:24 AM6/27/02
to
Thanks Jim,

But........

The VB listing at the bottom uses a WriteFile method, whereas ideally I want
to write Streams directly from the database rather than mess around with
temporary files. On top of this,

The WriteFile method in the example is really only valid for small files.
While it sends the file as an output stream, it doesn't seem to be able to
handle files larger than system memory, which means Gb size files are out of
the question.

Regards

Patrick

"Jim Ross [MVP]" <ro...@lotsofspamthroughhere.com> wrote in message

news:dmjjhu8arim8er836...@4ax.com...

0 new messages