Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Download BLOB from DB in proper format 2

Status
Not open for further replies.

cmv131

MIS
Feb 16, 2005
158
US
Is there a way to use ASP to create a link to download a blob field in its proper format?

I have a table that has filename and fileblob stored. I want a link tied to the name to download the actual file...

I know I can do
Code:
<a href="xx"> <% rs.filename %> </a>
but what would I put in the href to get it to download the file?
 
Make the link point to an empty folder on your web page...then make a custom 404 page for that folder.

In your custom 404 page, read the Request.ServerVariables to get the search key from the link's URL. Use the key to retrieve the database record. Use the recordset's .GetChunk method to pull the blob out and write it to the Response object for your download.
 
Actually that brings up a good point... why both using a blob when you could just store the files as normal and just keep the file paths in the database?
 
We have a 3rd party system which stores attachments as blobs, and does not store them on the server. We have had a few requests to get soft copies of all the attachments. So my line of thinking was to just provide the people who need it, an ASP page with links to all the files, and let them download at their leisure. It is esier to do that way then to go through the system itself and detach them. But of course, I would rather run some sort of script that saves them all to file with proper file names etc...Though how to do that is also beyond my current abilities.

Can I use GetChunck within a loop to save the files to a given directory? (in tandum with something like mysmartupload?)

I would much rather store the paths in the DB and the fileds on the server, unfortunatly it isn't possile in our situation
 
That seems to cover what I was looking for. Thanks so much
 
now my code looks like this... and I keep getting Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'BlobToFile'

Trying to pull from MS Access with datatype of OLE Object

Any idea who it wouldn't work?

Code:
   <%@ Language=VBScript %>
   <HTML>
   <BODY bgcolor=white>
   <%     
        Set cn = Server.CreateObject("ADODB.Connection")
        Set rs = Server.CreateObject("ADODB.Recordset")
        cn.Open "dsn=Attach"  
	SQL = "SELECT FILESIZE, FILENAME, FILEBLOB FROM Attachments"
        rs.Open SQL, cn
	rs.Movefirst
	while not rs.EOF
		BlobToFile RS("FILEBLOB"), "c:\BLOB\" & rs("FILENAME"), RS("FILESIZE")
  		rs.MoveNext
    	Wend
	rs.close
	cn.close
   %>
   </BODY>
   </HTML>
 
do you have the sub routine for BlobToFile on your page...

its in that link...

-DNG
 
Yeah I'll learn how to read one of these days...lol

Now to translate it from vb to vbscript...always fun...Thanks again

Chris
 
no problem..glad to be of help...post back if you have questions...

-DNG
 
Just a couple of thoughts:

The code above posted 16:34 by cmv131 seems to be selecting all records and creating files for all of them .. is this what is intended ? If so I am assuming it is a one off ? Otherwise it would be horridly inefficient to do this every time someone requests their specific blob !

So, if you haven't already, put a where clause in there.

If you go this route you may also want to do a quick check if the file exists before re-building it. Use the FileSystemObject's .FileExists property to check, and if it does, don't bother re-writing to disk. Though it does depend on whether these blob's change regularly or not.

The simplest way for this approach is to write whenever it populates the database, which means the download page can be made quite simple. Sounds like you may not have control over this though.

However, I would personally use an alternative method. This would involve streaming content directly into the response and modifying the http headers to tell the browser that it is a specific type of file (and to download it etc).

example code snippet:

Code:
dim oRS, sSQL, oConn

set oConn = Server.CreateObject("ADODB.Connection")
set oRS = Server.CreateObject("ADODB.Recordset")

oConn.open "your-connection-string"

sSQL = "SELECT FILENAME, FILETYPE, FILEBLOB FROM Attachments WHERE UniqueIDField = 123"

oRS.Open sSQL, oConn, adOpenKeyset, adLockOptimistic [COLOR=green]'You will need to ensure the ADO constants are loaded here.  you need either a keyset or static for the get chunk method later to work correctly [/color]

if oRS.EOF and oRS.BOF then
 response.write "No Data"
 response.end
end if

oRS.movefirst

[COLOR=blue]THE IMPORTANT COUPLE OF LINES[/color]
response.ContentType = "application/x-whatever-your-file-type-is" [COLOR=green]'you can use the filetype in the db, or the file extension to decide this - or just have an x-misc value so that it always downloads, it's not so important[/color]
Response.AddHeader "content-disposition","attachment; filename=" & oRS.Fields("FILENAME") & ";"

dim lSize, lChunk, lPos

lSize = oRS.Fields("FILEBLOB").ActualSize
lChunk = 1000 [COLOR=green]'Whatever chunk size you want, change this to optimise etc[/color]
lPos = 0
while lPos < lSize
  response.write(oRS.Fields("FILEBLOB").GetChunk(lChunk))
  lPos = lPos + lChunk
wend

oRS.close : set oRS = nothing
oConn.close : set oConn = nothing

The reason I've used the GetChunk method here rather than just dumping the entire file is for load / processing efficiency - though if most of the files are a few KB in size then don't bother - just dump the entire field as normal, but when you have MB's to deal with it can start to overload the server under high usage.

You'll probably notice that there is no HTML, at all - this is the idea, you're sending the blob as the content and telling the browser via the http header that it is a downloadable file, and not html. Therefore I would suggest that you open this page as a new window / pop up, using normal GET or POST variables to tell the page which blob to get. In most browsers this results in a download dialog box only (no new window) leaving the initial window intact.

I've written the above code on the fly, so you'll need to adapt it to your requirements, test it thoroughly, and watch out for typo's..

Hope that helps.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top