Return File. Problem with Filename?

Last post 10-22-2009 5:01 PM by levib. 9 replies.

Sort Posts:

  • Return File. Problem with Filename?

    10-20-2009, 9:56 AM
    • Contributor
      3,572 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,981

    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

  • Re: Return File. Problem with Filename?

    10-20-2009, 1:33 PM
    • Contributor
      5,676 point Contributor
    • Eilon
    • Member since 06-26-2002, 6:14 PM
    • Redmond, WA
    • Posts 966
    • AspNetTeam

    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 

    Blog: http://weblogs.asp.net/LeftSlipper/
  • Re: Return File. Problem with Filename?

    10-20-2009, 2:01 PM
    • Contributor
      3,572 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,981

    Eilon:

    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?

    Thanks,

    Miguel

  • Re: Return File. Problem with Filename?

    10-20-2009, 2:14 PM
    • Contributor
      3,572 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,981

    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


  • Re: Return File. Problem with Filename?

    10-22-2009, 2:38 AM

    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");
            }
           
        }
    }
    


  • Re: Return File. Problem with Filename?

    10-22-2009, 6:27 AM
    • Contributor
      3,572 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,981

    KeFang Chen - MSFT:
    It seems that you just want to return a file to be downloaded by user.

    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

  • Re: Return File. Problem with Filename?

    10-22-2009, 1:14 PM
    Answer
    • Contributor
      5,676 point Contributor
    • Eilon
    • Member since 06-26-2002, 6:14 PM
    • Redmond, WA
    • Posts 966
    • AspNetTeam

    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

    Blog: http://weblogs.asp.net/LeftSlipper/
  • Re: Return File. Problem with Filename?

    10-22-2009, 1:27 PM
    • Member
      7 point Member
    • AlexandroRR
    • Member since 07-14-2009, 2:47 PM
    • Posts 6

    Try to convert to UTF-7 Encoding:
    Convert.UFT7.FromBytes(Convert.UFT8.GetBytes("Ñ"))

    Filed under: ,
  • Re: Return File. Problem with Filename?

    10-22-2009, 2:57 PM
    • Contributor
      3,572 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,981

    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

  • Re: Return File. Problem with Filename?

    10-22-2009, 5:01 PM
    Answer
    • Contributor
      5,153 point Contributor
    • levib
    • Member since 07-23-2007, 11:50 PM
    • Redmond, WA
    • Posts 842

    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.

Page 1 of 1 (10 items)