Download data from a database in chunks

I am getting file data from a binary field in a database and I would like to pass it down to the client in chunks I found a piece of code written in 2003 I am trying to adopt for this purpose but I am getting 2 erros on this one line of code. I am wondering if you could help.

The errors are"
1 Number of indices exceeds the number of dimensions of the indexed array
2 Value of type '1-dimensional array of Byte' cannot be converted to 'Integer
Dim filedata As Byte() = CType(image.fileData.ToArray, Byte())
Context.Response.ContentType = image.fileType
Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + image.fileName)
Dim buffer(chunkSize) As Byte
Dim idx As Long = 0
Dim size As Long = 0
 
While (size <= filedata(1, idx, buffer, 0, ChunkSize)) =   ChunkSize '<=== Error here
    Context.Response.BinaryWrite(buffer)
    idx += chunkSize
End While
                            'Write the last bytes.
Dim remaining(size) As Byte
Array.Copy(buffer, 0, remaining, 0, size)
Context.Response.BinaryWrite(remaining)
Context.Response.End()

Open in new window

LVL 4
ruffoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joel CoehoornDirector of Information TechnologyCommented:
Re-write the while loop this way:
While idx + chunkSize < filedata.Length
    Array.Copy(filedata, idx, buffer, 0, chunkSize)
    Response.BinaryWrite(buffer)
    idx += chunkSize
End While
Array.Resize(buffer, filedata.Length - idx)
Array.Copy(filedata, idx, buffer, 0, filedata.Length - idx)
Response.BinaryWrite(buffer)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ruffoneAuthor Commented:
I only get the first 38 - 51k of the files and then it terminates
0
Joel CoehoornDirector of Information TechnologyCommented:
Try using filedata.LongLength instead of filedata.Length.

If that doesn't help, post back here with the error you get.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

ruffoneAuthor Commented:
that did not help. I found this link from another post on this site. I don't know if it will shed some light on our situation. My data is coming from a system.data.linq.binary and not from a reader like those in the link so I am not sure how to translate
http://support.microsoft.com/kb/317034/en-us
0
Joel CoehoornDirector of Information TechnologyCommented:
It's not really doing anything different than you are.  Have you tried this on a smaller file (<38K).  If so, does it complete, or does it perhaps fail a certain percentage of the way through?  Or are we perhaps incorrectly send an endoffile character or some such through?

BTW, I assume you're doing the chunking just save on the response buffer, but I think binary write bypasses that buffer.  If I'm write about that, since you're already converting the entire image to byte array in memory you might just as well do this:

    Response.ContentType = image.fileType
    Response.AddHeader("Content-Disposition", "attachment;filename=" + image.fileName)
    Response.BinaryWrite(CType(image.fileData.ToArray, Byte()))

A whole lot simpler, woudn't you agree?
0
Joel CoehoornDirector of Information TechnologyCommented:
Hmm..  a quick check shows you can just set Response.Buffer to false.
0
Joel CoehoornDirector of Information TechnologyCommented:
Also, depending on the nature of your image class, you might be able to use DirectCast() instead of CType, would perform a little better.
0
ruffoneAuthor Commented:
I am doing this now. The problem is that when I try to download a 700mb file by the time it completes I get an out of memory warning. this is why I am looking for another solution.

Response.Clear()
Response.ContentType = image.fileType '"application/vnd.ms-word.document.12"
.AddHeader("Content-Disposition", "attachment;filename=" + image.fileName)
Response.BinaryWrite(filedata)
Response.End()

Open in new window

0
Joel CoehoornDirector of Information TechnologyCommented:
Try it with response.Buffer = false right after response.clear?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.