Stream Any File Type

G

Grant Merwitz

Hi

I am trying to make a page to allow users to download files.
These files are not directly accessible through IIS, the code accesses the
file then streams this to the user.

One method i have tried, is to stream the file to the user.
Like so:


using(System.IO.FileStream fs = new System.IO.FileStream("c:\test.txt",
System.IO.FileMode.Open))
{
byte[] Buffer = new byte[(int)fs.Length];
fs.Read(Buffer, 0, (int)fs.Length);

//Write it Out
Response.CearContent();
Response.AppendHeader("Content-Disposition",
"inline;filename=test.txt");
Response.BinaryWrite(Buffer);
}

Now this works fine for text types (Word Doc, Pdf, TextFile),
but does not work with all formats (.zip, .wma)

I can right click the link that redirects to this page, but i have to change
the extension from .htm to is real type, and then it works fine.

Is this the right way to do this? Is there an easier way?
How can stream the correct file type?

TIA
 
P

Paul Henderson

Is this the right way to do this? Is there an easier way?
How can stream the correct file type?

That method is basically fine; you may find it works with binary
formats if you change the content type to something appropriate;
application/octet-stream should send plain binary data. So add

Response.ContentType = "application/octet-stream";

before you write the file. If you know the type of the file in advance,
you could instead use the correct MIME type.
 
G

Grant Merwitz

Thanks Paul

I've created a switch statement with all FileTypes i can think of to set
them correctly.
If not found, i set it to "application/none" which seems to work fine

would "application/octet-stream" be better?
 
P

Paul Henderson

would "application/octet-stream" be better?

I think application/none is non-standard (not in the 'official' IANA
list), but it shouldn't really matter either way. I think a browser is
probably obliged to use all eight bits for octet-stream; this may not
be so for application/none, but if it works when you test it, there
should be no problem.
 
G

Grant Merwitz

Great, thanks for you help Paul

Paul Henderson said:
I think application/none is non-standard (not in the 'official' IANA
list), but it shouldn't really matter either way. I think a browser is
probably obliged to use all eight bits for octet-stream; this may not
be so for application/none, but if it works when you test it, there
should be no problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top