FileContentResult and international characters

I am using fileContentResult to render a file in a browser. It works fine, except that it throws an exception when the filename contains international characters. I remember reading somewhere that this function does not support international characters, but I’m sure that when an application needs to upload files in countries other than the US, there should be a workaround or best practice experience.

Does anyone know about this practice? Here is the ActionResult method

public ActionResult GetFile(byte[] value, string fileName)
    {
        string fileExtension = Path.GetExtension(fileName);
        string contentType = GetContentType(fileExtension); //gets the content Type
        return File(value, contentType, fileName);
    }  

Thank you in advance

Susan

+5
source share
4 answers
public class UnicodeFileContentResult : ActionResult {

    public UnicodeFileContentResult(byte[] fileContents, string contentType) {
        if (fileContents == null || string.IsNullOrEmpty(contentType)) {
            throw new ArgumentNullException();
        }

        FileContents = fileContents;
        ContentType = contentType;
    }

    public override void ExecuteResult(ControllerContext context) {
        var encoding = UnicodeEncoding.UTF8;
        var request = context.HttpContext.Request;
        var response = context.HttpContext.Response;

        response.Clear();
        response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", (request.Browser.Browser == "IE") ? HttpUtility.UrlEncode(FileDownloadName, encoding) : FileDownloadName));
        response.ContentType = ContentType;
        response.Charset = encoding.WebName;
        response.HeaderEncoding = encoding;
        response.ContentEncoding = encoding;
        response.BinaryWrite(FileContents);
        response.End();
    }

    public byte[] FileContents { get; private set; }

    public string ContentType { get; private set; }

    public string FileDownloadName { get; set; }
}
+6
source

, . Content-disposition, , HTTP-, , ASCII, .

, , (application/www-url-encoded)

0
0
public FileContentResult XmlInvoice(Order order)
{
   string stream = order.Win1250StringData;
   var bytes = Encoding.GetEncoding("windows-1250").GetBytes(stream);
   var fr = new FileContentResult(bytes, "application/xml");
   fr.FileDownloadName = string.Format("FV{0}.xml", order.DocumentNumber);
   return fr;
}

UTF-8 Win1250 . , .

0

All Articles