473,396 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Stream Any File Type

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
Jan 5 '06 #1
4 1438
> 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.

Jan 5 '06 #2
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?

"Paul Henderson" <pa***********@pittville.demon.co.uk> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.

Jan 5 '06 #3
> 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.

Jan 5 '06 #4
Great, thanks for you help Paul

"Paul Henderson" <pa***********@pittville.demon.co.uk> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.

Jan 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: microsoft | last post by:
Hi, I have an ASP script that initiates a ADODB.Stream like below first bit gets filename from the database strAbsFile = getfilefromDB(request.querystring("fileid")) '-- create FSO object...
5
by: Daniel Corbett | last post by:
I am trying to save a file dynamically created in a webpage. I get the following headers, but cannot figure out how to save the attachment. I am basically trying to replicate what internet...
4
by: Samuel R. Neff | last post by:
I'm deserializing an XML file. If I pass a Stream to the file directly to the deserializer as follows it works fine: o = (New XmlSerializer( GetType(...
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
9
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
2
by: Cuong.Tong | last post by:
Greeting, Can anyone give me some hints about parsing the mulitpart/form-data stream? I have a form looks something like this <form action="process.dll> <input type=file...
5
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, The following code was suggested by one of the users in this newsgroup when a pdf file was requested by a user from an asp page. I used the similar code in my page and the very interesting...
0
by: prasenjit2007 | last post by:
I have a main form for inputing the (to/from/mesg/file) with the following code:- <html> <body> <table> <tr> <td>To:</td> <td><input type="text" name="to" size="50" ...
10
by: =?Utf-8?B?SnVhbg==?= | last post by:
Hi! I want to use ASP to download big files using ADODB.STREAM. It works very fine with files smaller than 80 MB. On the Webserver I can see that memory allocation and the process w3wp is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.