Streaming a file Thread Exceptions out

E

Eric Sabine

I have a web site where the user is able to download a file. I can't use an
anchor tag because I don't want the user to see where the filename is coming
from. Below is the code in which the user clicks to get the file. I could
swear this used to work but it definitely doesn't now. Each time at the
Response.End() method, I get a System.Threading.ThreadAbortException. I'm
not really sure why it is happening. Any clues?
Eric

Dim fStream As New System.IO.FileStream("c:\test.htm", IO.FileMode.Open,
IO.FileAccess.Read, IO.FileShare.Read)
Dim b(CType(fStream.Length, Int32)) As Byte
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/unknown"
Response.AddHeader("Content-Disposition", "attachment;filename=c:\test.htm")
fStream.Read(b, 0, CType(fStream.Length, Int32))
Response.BinaryWrite(b)
Response.End()
fStream.Close()
fStream = Nothing
 
K

Kevin Spencer

Take the Response.End() out of it. That method raises the exception, and is
unnecessary. In fact, all of the clearing of the Response is also
unnecessary, unless you've already sent some of the Response.

--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
E

Eric Sabine

Kevin, I took out the .end method and much to my surprise, now it streams
the text of my index.aspx page. So for example, I am testing with a text
file called test.htm. It's content is only the word test. When my code bit
from below runs, the browser pops up a save/open dialogue. The file name
suggested is "index" with no known extension. If I save the file and open
it, I see the following

test
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>File Transferring Page</title>

etc.

My whole index.aspx is streamed.

Any ideas?

Eric
 
K

Kevin Spencer

Looks like you've still got some HTML in your Page Template (ASPX file). If
you remove everything but the @Page directive, it should be fine.

--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
E

Eric Sabine

Wow that doesn't make sense to me. My index.asp starts out like this

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="index.aspx.vb"
Inherits="filetransfer.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>File Transferring Page</title>
etc.

What are suggesting I remove? Most everything in the index.ASPX file is
HTML. But I don't thing that's what you meant.

Please check this thread back on Monday as I'm about to walk away from my PC
until then.

Thanks
Eric
 
K

Kevin Spencer

What are suggesting I remove? Most everything in the index.ASPX file is
HTML. But I don't thing that's what you meant.

That's what I meant. The HTML that VS.Net adds to your page is due to the
fact that an HTML document is what an ASPX page returns ALMOST all the time.
Streaming a file is one of the exceptions. In that case, you don't want any
additional content streamed to the browser; just what you send via your
code.

--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

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,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top