|
-
May 15th, 2006, 10:26 AM
#1
Thread Starter
Frenzied Member
File download - does not exist in temporary files
Hi
I'm streaming a file down to the browser as it is stored outside the website path using the following method:
Code:
FileInfo file = new FileInfo(ConfigurationSettings.AppSettings["DocumentLibraryPath"] + lblDocumentUrl.Text);
Stream iStream = null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;
try {
iStream = new FileStream(file.FullName,
FileMode.Open,
FileAccess.Read,
FileShare.Read);
dataToRead = iStream.Length;
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", iStream.Length.ToString());
Response.ContentType = "application/octet-stream";
while (dataToRead > 0) {
if (Response.IsClientConnected) {
length = iStream.Read(buffer, 0, 10000);
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
buffer= new Byte[10000];
dataToRead = dataToRead - length;
} else {
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
Response.End();
}
catch (Exception ex) {
lblDownloadError.Text = String.Format("<p>Download error: {0}</p>", ex.Message.Replace(ConfigurationSettings.AppSettings["DocumentLibraryPath"], ""));
}
finally {
if (iStream != null) {
iStream.Close();
}
}
As I use the content-type application/octet-stream it forces the Open, Save or Cancel dialog to be shown.
This works fine when you choose to save. However sometimes if you choose to open the file the application that tries to open it (tried Acrobat, Word and Powerpoint) says the file cannot be found. It's like the file is deleted from the temporary files before the application can launch it.
Anyone experienced this - any suggestions?
DJ
Last edited by dj4uk; May 16th, 2006 at 03:14 AM.
If I have been helpful please rate my post. If I haven't tell me!
-
May 15th, 2006, 12:54 PM
#2
Re: Streaming files
Just so you know, this is not actually "streaming". Streaming is when you send data that gets discarded immediately so that it cannot be saved.
Try taking the response.isclientconnected portion out and see if the same thing still occurs.
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
May 16th, 2006, 03:09 AM
#3
Thread Starter
Frenzied Member
Re: Streaming files
I stand corrected - wasn't actually sure what to call what I'm trying to do! 
I'll give it a go.
Dave
If I have been helpful please rate my post. If I haven't tell me!
-
May 16th, 2006, 03:13 AM
#4
Thread Starter
Frenzied Member
Re: Streaming files
Nah that makes no difference. Any other ideas?
I'm even open to other suggestions of how I serve the file back to the browser.
Dave
If I have been helpful please rate my post. If I haven't tell me!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|