0

I am using the following code to write the contents of a string (converted to a byte array) to the client in ASP.NET/C#

byte[] data = StrToByteArray(strData);
Response.ClearContent();
Response.AppendHeader("content-length", data.Length.ToString());
Response.ContentType = "text/plain";
Response.AppendHeader("content-Disposition", "attachment;filename=" + fileName);
Response.BinaryWrite(data);
Response.Flush();

fileName is the name of the file ending with the file extension (.pgn). However, the file is saved as a .txt file, ignoring the extension that I am giving it. Would this have to do with the Response.Contenttype = "text/plain"? How can I get the Open/Save dialog to display and save the correct (.pgn) filename?

Also, if filename is a string separated by dashes or spaces, when the Open/Save dialog comes up, the filename is not displayed in its entirety but it is truncated where the first dash (-) or space (or comma) is encountered. How can this be remedied?

Rob
  • 43,549
  • 23
  • 115
  • 144
Tim
  • 51
  • 2

1 Answers1

0
  1. Yes, it is saving .txt because of your Content Type (MIME type). Use image/png.

  2. How about you remove the dashes and spaces? String.Replace is great. fileName.Replace("-", ""); etc.

TheGeekYouNeed
  • 7,449
  • 2
  • 24
  • 42
  • Hi thanks, I have used the replace, but the file is indeed a text file but with the "pGn" extension (not png). In IE it is saving correctly, but in Firefox it is saving it as .png.txt – Tim Aug 08 '10 at 18:52
  • I solved it in the end. I had to use "application/octet-stream" as the ContentType if I wanted to use the different extension. – Tim Aug 08 '10 at 19:37