1

I am storing some the binary data for some files in a database (yes, I know this can be a bad idea). I am able to get the files out, with the correct content type, since I have stored it. But I'm having trouble getting the file to the client with the right filename. Right now I have the following code in a file called get_file.asp:

sSQL = "SELECT filename, contenttype, binarydata FROM new_attachment WHERE filename = '" & filename & "'"

oRs.Open sSQL, conn, 3, 3

If Not oRs.EOF Then
    Response.ContentType = oRs(1)
    Response.BinaryWrite oRs(2)

End if

This will return files correctly, but with the filename of 'get_file.asp', instead of, say, 'myfile.txt'. The url visited is .../get_file.asp?filename=myfile.txt.

Is there a way I could change the name of the file when the browsers prompts the user to save it somewhere?

Indigenuity
  • 9,332
  • 6
  • 39
  • 68
  • I was dealing with a similar issue here: http://stackoverflow.com/questions/440892/how-to-output-an-excel-xls-file-from-classic-asp – cdeszaq Jan 18 '12 at 16:30

2 Answers2

2

You need to send out the correct header:

Response.ContentType = "text/html"
Response.AddHeader "Content-Disposition", "attachment; filename=YOURFILE.TXT"
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
1

You need to add a Content-Disposition header.

The header should look like this:

Content-Disposition: attachment; filename=<name of file>

Side note: The way you are concatenating SQL is open to SQL Injection - you should be using parameters.

Oded
  • 489,969
  • 99
  • 883
  • 1,009