XamDev
XamDev

Reputation: 3647

Pass non ascii characters in response headers

I am trying to download file where in some file it contains non ascii characters, at this time download gets failed. It gives the below error.

Invalid non-ASCII or control character in header: 0x000D

The code works well with ascii characters. I am passing the file name in response header to UI which is developed in angular 6.

Below is my code

[HttpGet]
        [Route("someroute/downloadfile")]
        public IActionResult DownloadFile(int ID)
        {
            try
            {
                    using (ExcelPackage pck = bal.DownloadFile(ID))
                    {
                        System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
                        {
                            FileName = pck.File.Name,
                        };

                        Response.Headers.Add("Content-Disposition", cd.ToString());

                        return File(pck.GetAsByteArray(), "application /vndopenxmlformats-officedocument.spreadsheetml.sheet");
                    }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

However, my requirement is I want to download the file as is though it has non ascii characters.

Any help on this appreciated.

Upvotes: 2

Views: 4958

Answers (1)

Lady TCT
Lady TCT

Reputation: 21

You could encode the non-ASCII header text as Base64, for example Convert.ToBase64String(Encoding.UTF8.GetBytes(dirtyString))).

In Angular you can use atob get the non-ASCII string back.

Upvotes: 2

Related Questions