That is a limitation in ASP.NET MVC and we will try to address it in an upcoming release. See more details here (and a workaround):
http://forums.asp.net/t/1448041.aspx
That is a limitation in ASP.NET MVC and we will try to address it in an upcoming release. See more details here (and a workaround):
http://forums.asp.net/t/1448041.aspx
Yes, I think I was one of the first finding that problem on one of the previous. I though it was solved ...
The workaround you mean is:
public ActionResult GetFile(byte[] value, string fileName)
{
string fileExtension = Path.GetExtension(fileName);
string contentType = GetContentType(fileExtension);
Response.Clear();
if (Request.Browser.Browser == "IE")//IE needs special handling in order to display the international
//characters in the file name
{
string attachment = String.Format("attachment; filename={0}", Server.UrlPathEncode(fileName));
Response.AddHeader("Content-Disposition", attachment);
}
else
Response.AddHeader("Content-Disposition", "attachment; filename="+fileName);
Response.ContentType = contentType;
Response.Charset = "utf-8";
Response.HeaderEncoding = UnicodeEncoding.UTF8;
Response.ContentEncoding = UnicodeEncoding.UTF8;
Response.BinaryWrite(value);
Response.End();
return null;
}
What about creating a new Action Result based on this code? Is it possible?
And is there a straigh way to convert international caracters like "ã", "á" to its base "a"; or Õ, Ó, etc to O?
But, for example, with document.Title = "Calculo Integral - Primitivação por partes" which should be the filename I get the file "Calculo" with nothing else not even the PDF extension.
It seems that you just want to return a file to be downloaded by user. If It is, you can take a look at the following code. I have tested it. It works well.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using MovieApp.Filters;
namespace MovieApp.Controllers
{
public class ImageDemoController : Controller
{
//
// GET: /ImageDemo/
[TestFilter]
public ActionResult Index()
{
string path = Server.MapPath("~/") + @"\nhibernate_reference.pdf";
FileStream fs = new FileStream(path, FileMode.Open);
byte[] datas = new byte[fs.Length];
fs.Read(datas, 0, Convert.ToInt32(fs.Length));
return File(datas,"application/pdf","test.pdf");//File(ms, "image/jpeg");
}
}
}
In ASP.NET MVC we're implementing RFC 2183, which only allows US ASCII characters. As it turns out, there is a new RFC that supercedes it, RFC 2231. If you want to support more than US ASCII characters you have to implement RFC 2231. That's a feature we
plan to fix in an upcoming release. For more info on this RFC, check out this page:
http://greenbytes.de/tech/webdav/draft-reschke-rfc2231-in-http-latest.html
As far as converting characters with diacritic and other marks to their "parent" characters, that's not a solution I would recommend. While such a solution might work with some characters, such as those found in Spanish, it won't work with characters from
languages based on completely different alphabets, such as Japanese, Arabic, or Russian.
Thanks,
Eilon
Blog: http://weblogs.asp.net/LeftSlipper/
Marked as answer by ricka6 on Oct 23, 2009 05:43 PM
The File() function in MVC currently cannot be coerced to outputting a filename using RFC 2231 format. If you want this functionality, you're going to have to write it from scratch unless it gets fixed in a future release.
Marked as answer by ricka6 on Oct 23, 2009 05:43 PM
shapper
Contributor
3932 Points
3789 Posts
Return File. Problem with Filename?
Oct 20, 2009 01:56 PM|LINK
Hello,
I have a Download action to return a file. I am doing it as follows:
return File(document.File, "application/pdf", "Pão Bons");
This is a test to replicate the error I was getting and which is the following:
Exception Details: System.FormatException: An invalid character was found in the mail header.
Is this because of the characters on the file name? Why mail header? That confused me?
How can I be assured that the file name will be valid?
Is there a conversion I can apply?
Thanks,
Miguel
Eilon
Contributor
5753 Points
976 Posts
Microsoft
Re: Return File. Problem with Filename?
Oct 20, 2009 05:33 PM|LINK
Hi Miguel,
That is a limitation in ASP.NET MVC and we will try to address it in an upcoming release. See more details here (and a workaround): http://forums.asp.net/t/1448041.aspx
Thanks,
Eilon
shapper
Contributor
3932 Points
3789 Posts
Re: Return File. Problem with Filename?
Oct 20, 2009 06:01 PM|LINK
Yes, I think I was one of the first finding that problem on one of the previous. I though it was solved ...
The workaround you mean is:
public ActionResult GetFile(byte[] value, string fileName) { string fileExtension = Path.GetExtension(fileName); string contentType = GetContentType(fileExtension); Response.Clear(); if (Request.Browser.Browser == "IE")//IE needs special handling in order to display the international //characters in the file name { string attachment = String.Format("attachment; filename={0}", Server.UrlPathEncode(fileName)); Response.AddHeader("Content-Disposition", attachment); } else Response.AddHeader("Content-Disposition", "attachment; filename="+fileName); Response.ContentType = contentType; Response.Charset = "utf-8"; Response.HeaderEncoding = UnicodeEncoding.UTF8; Response.ContentEncoding = UnicodeEncoding.UTF8; Response.BinaryWrite(value); Response.End(); return null; }What about creating a new Action Result based on this code? Is it possible?
And is there a straigh way to convert international caracters like "ã", "á" to its base "a"; or Õ, Ó, etc to O?
Thanks,
Miguel
shapper
Contributor
3932 Points
3789 Posts
Re: Return File. Problem with Filename?
Oct 20, 2009 06:14 PM|LINK
Hello,
I tried the following:
Document document = _documentService.GetById(id); Response.Clear(); if (Request.Browser.Browser == "IE") { String attachment = String.Format("attachment; filename={0}", String.Concat(document.Title, ".pdf")); Response.AddHeader("Content-Disposition", attachment); } else Response.AddHeader("Content-Disposition", "attachment; filename=" + String.Concat(document.Title, ".pdf")); Response.ContentType = "application/pdf"; Response.Charset = "utf-8"; Response.HeaderEncoding = UnicodeEncoding.UTF8; Response.ContentEncoding = UnicodeEncoding.UTF8; Response.BinaryWrite(document.File); Response.End(); return null;But, for example, with document.Title = "Calculo Integral - Primitivação por partes" which should be the filename I get the file "Calculo" with nothing else not even the PDF extension.
Any idea?
Thanks,
Miguel
KeFang Chen ...
Star
8329 Points
852 Posts
Re: Return File. Problem with Filename?
Oct 22, 2009 06:38 AM|LINK
Hi shapper,
It seems that you just want to return a file to be downloaded by user. If It is, you can take a look at the following code. I have tested it. It works well.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; using System.Drawing; using System.Drawing.Imaging; using System.IO; using MovieApp.Filters; namespace MovieApp.Controllers { public class ImageDemoController : Controller { // // GET: /ImageDemo/ [TestFilter] public ActionResult Index() { string path = Server.MapPath("~/") + @"\nhibernate_reference.pdf"; FileStream fs = new FileStream(path, FileMode.Open); byte[] datas = new byte[fs.Length]; fs.Read(datas, 0, Convert.ToInt32(fs.Length)); return File(datas,"application/pdf","test.pdf");//File(ms, "image/jpeg"); } } }shapper
Contributor
3932 Points
3789 Posts
Re: Return File. Problem with Filename?
Oct 22, 2009 10:27 AM|LINK
No the problem is not that.
The problem is if the filename has characters like "ã, ó, etc".
Please, read the previous posts.
I know how to return the file.
Thanks,
Miguel
Eilon
Contributor
5753 Points
976 Posts
Microsoft
Re: Return File. Problem with Filename?
Oct 22, 2009 05:14 PM|LINK
Here are some more details.
In ASP.NET MVC we're implementing RFC 2183, which only allows US ASCII characters. As it turns out, there is a new RFC that supercedes it, RFC 2231. If you want to support more than US ASCII characters you have to implement RFC 2231. That's a feature we plan to fix in an upcoming release. For more info on this RFC, check out this page: http://greenbytes.de/tech/webdav/draft-reschke-rfc2231-in-http-latest.html
As far as converting characters with diacritic and other marks to their "parent" characters, that's not a solution I would recommend. While such a solution might work with some characters, such as those found in Spanish, it won't work with characters from languages based on completely different alphabets, such as Japanese, Arabic, or Russian.
Thanks,
Eilon
AlexandroRR
Member
7 Points
6 Posts
Re: Return File. Problem with Filename?
Oct 22, 2009 05:27 PM|LINK
Try to convert to UTF-7 Encoding:
Convert.UFT7.FromBytes(Convert.UFT8.GetBytes("Ñ"))
UFT-8 UTF-7
shapper
Contributor
3932 Points
3789 Posts
Re: Return File. Problem with Filename?
Oct 22, 2009 06:57 PM|LINK
Yes,
I tried:
Byte[] filename = System.Text.Encoding.UTF8.GetBytes(String.Format("{0} (Web Site Name).pdf",document.Title)); String name = HttpUtility.UrlEncode(filename); return File(document.File, "application/pdf", name);But I ended up with a lot of % and+ in the filename
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Return File. Problem with Filename?
Oct 22, 2009 09:01 PM|LINK
The File() function in MVC currently cannot be coerced to outputting a filename using RFC 2231 format. If you want this functionality, you're going to have to write it from scratch unless it gets fixed in a future release.