Unicode Headers for Filedownload

W

WO70

Hi,

maybe someone had the same problem:
http://support.microsoft.com/kb/895262/en-us

This Hotfix is only available for Windows 2003, but we also use Windows
2000 and for development Windows XP. So I thought ASP.NET 2.0 will make
things better.

And now ASP.NET is there and I found this:
http://msdn2.microsoft.com/en-us/library/system.web.httpresponse.headerencoding.aspx

But if I set the Encoding to Unicode, I get this:
'utf-16' cannot be used as Header Encoding

Here is a little testing app:
string filename = "abcdeäüö.txt";

System.Text.UnicodeEncoding enc = new
System.Text.UnicodeEncoding();

Response.ContentType = "text/plain";
Response.Clear();
byte[] buffer ={ 65 };
Response.HeaderEncoding = enc;
Response.AddHeader("Content-Disposition",
"attachment;filename=" + filename);
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();

Is it a bug or a feature? ;-)

thx in advance
Bjoern Wolfgardt
 
J

Joerg Jooss

WO70 said:
Hi,

maybe someone had the same problem:
http://support.microsoft.com/kb/895262/en-us

This Hotfix is only available for Windows 2003, but we also use
Windows 2000 and for development Windows XP. So I thought ASP.NET 2.0
will make things better.

And now ASP.NET is there and I found this:
http://msdn2.microsoft.com/en-us/library/system.web.httpresponse.heade
rencoding.aspx

But if I set the Encoding to Unicode, I get this:
'utf-16' cannot be used as Header Encoding

Here is a little testing app:
string filename = "abcdeäüö.txt";

System.Text.UnicodeEncoding enc = new
System.Text.UnicodeEncoding();

Response.ContentType = "text/plain";
Response.Clear();
byte[] buffer ={ 65 };
Response.HeaderEncoding = enc;
Response.AddHeader("Content-Disposition",
"attachment;filename=" + filename);
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();

Is it a bug or a feature? ;-)

It's a bug. HeaderEncoding doesn't work at all -- well, the
undocumented exception in case you try to use UTF-16 is being thrown,
but sadly that's it.

This is a IE bug as well -- Firefox has no problems decoding the UTF-8
encoded header.

Cheers,
 
Joined
Aug 25, 2006
Messages
1
Reaction score
0
a kind of solution

Hi,

You should try this:

string att = String.Format( "attachment; filename=\"{0}\"", Server.UrlPathEncode( filename ) );
Response.AddHeader( "Content-Disposition", att );


It may fix the problem. Or in Korea it may not.

Best regards
gezsi
 
Joined
Dec 10, 2007
Messages
1
Reaction score
0
protected void btnSaveExcel_Click(object sender, EventArgs e)
{
setGridView2DataBainding();
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+filename);
System.Web.HttpContext.Current.Response.ContentType = "application/unknown";
System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content='text/html; charset=utf-8'>");
System.IO.StringWriter sWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter= new System.Web.UI.HtmlTextWriter(sWriter);
setGridView2DataBainding();
GridView2.RenderControl(htmlWriter);
GridView2.AllowPaging = true;
System.Web.HttpContext.Current.Response.Write(sWriter.ToString());
System.Web.HttpContext.Current.Response.End();
HttpContext.Current.Response.End();
}
 
Joined
Mar 23, 2009
Messages
1
Reaction score
0
Hi

string att = String.Format( "attachment; filename=\"{0}\"", Server.UrlPathEncode( filename ) );
Response.AddHeader( "Content-Disposition", att );


The above code helps but there's one issue when we directly open the content file from Temp directory the file name is stii in Hexadecimal encoding.:-(

Is there any help
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top