1

I'm pushing a PEM file towards my website user for download. here is the code:

try { FileStream sourceFile = null; Response.ContentType = "application/text"; Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFileName(RequestFilePath)); sourceFile = new FileStream(RequestFilePath, FileMode.Open); long FileSize = sourceFile.Length; byte[] getContent = new byte[(int)FileSize]; sourceFile.Read(getContent, 0, (int)sourceFile.Length); sourceFile.Close(); Response.BinaryWrite(getContent); } catch (Exception exp) { throw new Exception("File save error! Message:<br />" + exp.Message, exp); } 

The issue is that the file that is downloaded has the content that should be there + a copy of the whole web page's HTML too.

Whats happening here?

3
  • Where is that code executing? In an HTTP handler, in a button click? Commented Jul 11, 2012 at 13:54
  • before setting response first clear it. Commented Jul 11, 2012 at 13:55
  • @HassanGulzar - +1 for your code. that helped me a lot. :) Commented Nov 28, 2012 at 4:53

3 Answers 3

5

Place the following...

Response.Clear(); 

Before...

Response.ContentType = "application/text"; 

Update

As @Amiram says in his comments (and I was about to add myself anyway)...

After...

Response.BinaryWrite(getContent); 

Add...

Response.End(); 
Sign up to request clarification or add additional context in comments.

3 Comments

Still the whole page get written.
@Hassan, do you mean that the whole page (as well as the contents of the file you are downloading) are being combined into the file being downloaded?
Yup, they were. First was the actual data I wanted followed by the rendered HTML of the whole page. Fixed now.
3

Add this line:

Response.ClearContent(); Response.ContentType = "application/text"; ... 

4 Comments

Still the whole page get written.
Add Response.End() at the end
That fixed it. Its now between Response.ClearContent() and Response.ContentType = "application/text"
I do notice that an exception is generated after Response.End()'. But the exception is undefined. It reads: ThreadAbortException`. Any ideas?
0

I agree with @Amiram Korach's solution.
That is add Response.ClearContent(); before Response.ContentType...

But as per your comment

Still the whole page get written

@Amiram Korach replied to add Response.End() at the end.
But it throws System.Threading.ThreadAbortException.

So I advice you to add additional catch to catch System.Threading.ThreadAbortException and don't put error message in Response.Write otherwise it will also be added to your text file:

try { FileStream sourceFile = null; Response.ClearContent(); // <<<---- Add this before `ContentType`. Response.ContentType = "application/text"; . . Response.BinaryWrite(getContent); Response.End(); // <<<---- Add this at the end. } catch (System.Threading.ThreadAbortException) //<<-- Add this catch. { //Don't add anything here. //because if you write here in Response.Write, //that text also will be added to your text file. } catch (Exception exp) { throw new Exception("File save error! Message:<br />" + exp.Message, exp); } 

This will solve your problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.