• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

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

0
ruffone
Asked:
ruffone
  • 6
  • 3
1 Solution
 
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
 
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
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now