ASP.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

ASP Source Code


Welcome to Dream.In.Code
Become a ASP.NET Expert!

Join 300,341 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 1,611 people online right now. Registration is fast and FREE... Join Now!





Downloading (pushing) large files

The response object has a built in maximum on memory usage. If you want to push files in the traditional way, that is read it in and then echo it out, you will run into this limit. This sniplet uses chunks to get around this problem. edit 10/09/09: fixed a bug!

Submitted By: Trogdor
Actions:
Rating:
Views: 1,841

Language: ASP

Last Modified: September 10, 2009
Instructions: Filepath should point to the file, Filename is the name used to save the file. (dont forget the extension)

Snippet


  1. sub SendFileToResponse(FilePath, FileName)
  2.      dim clChunkSize, oStream, length
  3.      
  4.       clChunkSize = 1048576
  5.      Response.Buffer = false
  6.  
  7.      Response.ContentType = "application/octet-stream"
  8.      Response.AddHeader "Content-Disposition", "attachment; Filename=" + FileName
  9.  
  10.      set oStream = Server.CreateObject("ADODB.Stream")
  11.      oStream.Type = 1
  12.      oStream.Open()
  13.      oStream.LoadFromFile(FilePath)
  14.  
  15.      Response.AddHeader "Content-Length", oStream.Size
  16.  
  17.      length = int (oStream.Size / clChunkSize)
  18.      if(length > 0) then
  19.           for i = 1 to length
  20.                Response.BinaryWrite oStream.Read(clChunkSize)
  21.           next
  22.      end if
  23.  
  24.      if ((oStream.Size mod clChunkSize) >0) then
  25.           Response.BinaryWrite oStream.Read(oStream.Size mod clChunkSize)
  26.      end if
  27.      oStream.Close()
  28. end sub

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live ASP.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

ASP.NET Tutorials

Reference Sheets

ASP.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month