Search code examples
angularjscontent-dispositionng-file-upload

How to set content disposition character encoding in ng-file-upload


I'm trying to upload a file with an em/en dash '–' character in its name (encoded in utf8). The character is rendered correctly in the browser request (looking at chromes network monitor). But no encoding information is sent with the request to the server, the character is then interpreted as 'â' (code page 28591, iso-8859-1) by the server. I've come to understand that using

filename*=UTF-8''myFileWith–In.pdf 

may work but can't seem to manipulate the filename attribute in a suitable way, for example not using double quotes. Source

My angular code looks something like this, is it possible from here to add encoding information to the content disposition?

function uploadFile(file, url)
{
     $upload.upload({
        url: url,
        method: 'POST',
        file: file,
     });

}

On the server I can use the following code to correct the encoding but it assumes that all incoming traffic was encoded in utf8, there needs to be a way of identifying the encoding in the request.

Encoding iso = Encoding.GetEncoding("ISO-8859-1");
Encoding utf8 = Encoding.UTF8;
byte[] isoBytes = iso.GetBytes(file.FileName);
string msg = utf8.GetString(isoBytes);

Solution

  • You can use fileName option of upload to set the content-disposition's filename. See the readme reference for explanation.

    function uploadFile(file, url)
    {
         $upload.upload({
            url: url,
            method: 'POST',
            file: file,
            fileName: 'UTF-8\'\'' + file.name
         });
    
    }