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!

Binary Stream Issues

Status
Not open for further replies.

DreXor

Programmer
Jun 17, 2003
2,224
US
Here's the code i'm using , problem i'm having is not getting the files in the browser or anything, it's coming up as download.asp for the filename on everything, or for say image types, right click and save as always is BMP format only and default name is untitled. is there any way to force filename? also MP3's and MPEGs for some reason are un-openable/readable.

thanks
DreX

Code:
<%
Response.Buffer = True

FilePath = Request("FullPath")

Response.Clear

Set Stream = Server.CreateObject("ADODB.Stream")
Set FS = CreateObject("scripting.FileSystemObject")
Set F = FS.GetFile(FilePath)
  FileSize = F.Size
  FileName = F.Name
  FilePath = F.Path
  Stream.Open
  Stream.Type = 1
  Stream.LoadFromFile FilePath

FileType = lcase(Right(FileName, 4))
ForceDownload = True
TextStream = False
Select Case FileType
  Case ".asf"
    ContentType = "video/x-ms-asf"

  Case ".avi"
    ContentType = "video/avi"

  Case ".zip"
    ContentType = "application/zip"

  Case ".xls"
    ContentType = "application/vnd.ms-excel"

  Case ".gif"
    ContentType = "image/gif"
    ForceDownload = False

  Case ".jpg", "jpeg",".jpe"
    ContentType = "image/jpeg"
    ForceDownload = False

  Case ".wav"
    ContentType = "audio/wav"

  Case ".mp3"
    ContentType = "audio/mpeg3"

  Case ".mpg", "mpeg",".mpe"
    ContentType = "video/mpeg"

  Case ".rtf"
    ContentType = "application/rtf"

  Case ".htm", "html"
    ContentType = "text/html"
    ForceDownload = False
    TextStream = True

  Case ".txt", ".cfg"
    ContentType = "text/txt"
    ForceDownload = False
    TextStream = True

  Case ".asp"
    ContentType = "text/asp"
    ForceDownload = False
    TextStream = True

  Case Else
    ContentType = "application/octet-stream"
End Select

If Not TextStream Then
  If ForceDownload Then
    Response.AddHeader "Content-Disposition", "attachment;filename=" & FileName
  Else
    Response.AddHeader "Content-Disposition", "filename=" & FileName
  End If
  Response.AddHeader "Content-Length", FileSize
  Response.Charset = "UTF-8"
  Response.ContentType = ContentType
  
  Response.BinaryWrite Stream.Read
  Response.Flush
Else
  Set F = FS.OpenTextFile(FilePath, 1,-2) 
%>
<html>
<body>
<xmp>
<%=F.ReadAll%>
</xmp>
</body>
</html>
<%
  F.Close
End If

Set F = nothing
Set FS = nothing
Stream.Close
Set Stream = Nothing
%>
 
Try clearing your temporary internet files. Any time they get full you start getting images that try to download as untitled.bmp and other strange effects. :)

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
been there tried that too, even added HTTP headers under IIS for the holding folder of download.asp to include cache-control : no-cache with little success.

thing that gets me is say a jpeg will download, open, and display properly in the browser, but, right click and saving it is untitled.bmp
 
That right click sve thing has always been because my temporary internet files folder is full. basically when you right click and save as it has to download a new copy of the file because it stops caching files locally when it is full.
Double check your temp intenert folder locally on your client machine, it's possible there is some stuff that wasn't deleted and is bloating the folder.
Your code looks fine, in fact it looks almost exactly like a simiar one I have on my machine except for the html and xmp tags (haven't tried that before).

Maybe try it without the tags, just stream the data directly...

Is it possible the line breaks before and after the file are causing it mess up? (after <xmp> before </xmp>)

I'll try it here locally and see what happens,
-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
I just tried a Gif file from my machine.
In FireFox right click to save image gives me the toption to savewith a .html extension
In IE 6 it gives me "sample.asp" as the filename but the two dropdown options are Gif and Bitmap

...same thing occursa without the <xmp> tags so that isn't it.

Odd.

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
the portion of the conditionals where the xmp tags are is for text stream items like ASP files for example, that way it's viewable and the browser isn't trying to translate it into viewable HTML ( like when you view an asp page with file:// instead of http:// ) and making it easier to copy paste, view/read.

 
This is interesting. Apparently that xmp tag was deperecated in the 3.0/.2 vesion of the HTML spec. and isn't included in the 4.0 spec. It's supposed to be used to show literal text content and if you view source (not availble in IE, available in FireFox) all you get is the img data, no html or xml tags.

Even though the result is the same (in terms of saving) without the tags, I would suggest switching to not using the tags to be more browser compatible. Some browsers already don't support the xmp tag.

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Oh, duh, I wasn't paying any attention to the conditional, no wonder I was confused :p

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Ok, problem solved :)

In order to test this I wrote a quick add-on to the script:
Code:
[b]Added to top:[/b]

If Request.Form("FullPath") = "" Then
	%>
	<html>
	<body>
	<form method="GET" action="sample.asp">
	<input type="text" name="FullPath"><input type="submit">
	</form>
	</body>
	</html>
	<%
Else

[b]Added to Bottom[/b]
End if

The fact that I made this little addition to make the thing easier to test is wat eventually gave me the answer.
If you run this thing and feed it the path to an image, then right click that image to save as, the filename comes up as sample.htm (I named it sample.asp, image was 4guyslogo.gif) and you have the option to save as gif or bmp in the dropdown.
So I changed the extension to gif and tried that. When I opened the file it gaveme a "Type Unrecognized" error and couldn't diosplay it. So I figured, what the heck, I'll try it as an htm document. Guess what, it displayed the initial form...in other words, instead of saving what was on te screen it went back out to get a fresh copy. Since it didn't repost the data to the address it was getting back my html in form.
In the case of Drexor's code it was receiving back absolutely nothing because he didn't have a case where it would display something to the screen if there was no form info posted.

So I tried switching my little form to GET and used QueryString instead. TaDa! I right clicked the image and it wanted me to save it as a Gif with the correct name! Double clicking my new desktop file gave me the picture! Woohoo!

Ok, well I wanted to check compatibility, so I trid it in FireFox which had been getting worse results than IE (it didn't give me the bmp/gif dropdown that IE gave me - course it wasn't working in IE even with the dropdown). With the new querystring method it worked like a charm, no problems.

Tried an Mp3: Worked in both browsers after adding double quotes to filename attachment tags to make up for spaces in name

Tried an ASP: Worked in both just fine

So it looks like what happens when you send dynamic content to the browser is that if the browsr displays it then right clicking on it and attempting to save it externally causes the browser to use the current address to go back and look for a fresh copy. If you have used form information providing that image/whatever then it will be lost. If you have querystring info then that is part of the address and is preserved in the call back for a fresh copy.

In the end I would suggest not passing a full path to the page because that tells people something about your drive setup, I would suggest having some base hardcoded paths and passing a key to describe the base path then additional sub paths that should be concatenated on to it. this way the user knows part of the path but nothing useful.

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
thank you much tarwn, so just a matter of flip flopping this around to method="get"?

new fun fun in the coding, have to avoid structures that exceed 240 sum odd chars to avoidexceeding the url limit.

 
yep, but like I said, hardcode the paths to some folders n only download form that folder. the all you have to do is provide a number (to specify which hardcoded folder) and the filename. Shouldn't be hard to keep that under 240 :)

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
problem is this is a remote file access environment over some 200 gigs of misc files, spanning a good handful of drives. That would turn out to a DB nightmare of adding/removing files as they come and go in order to mainatain some index values to point at folders.

i'll see what i can figure out :)

thanks again
 
I had a client that wanted the following for their internet portal:

1) Admin ability to profile, per user, a small set of UNC paths that contain files the users would have full access to.

2) Present an "explorer" interface with navigation pane on the left of expandable and collapsable folders (virtually stitched together from #1), and a contents pane on the right that lists the folder contents in a detail view mode.

I used frames for the panes, and a folders.asp page for the left and a files.asp page for the right. Wired them together with cross-frame communication; javascript+DHTML for the expand/collapse; file system object for folder expansion (along with a recursive loop for drilling into subfolders); file system object for listing the files; some paintshop work for getting small icons of a handful of known, supported file types (doc, xls, pdf, etc).

Oh, and I developed an activex pluggin that would allow an admin to browse UNCs for selection in the admin page that sets up UNCs for each customer.

It works quite well.

Regards,
TR



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top