0

I have the following function to download files from our server. Some customers name their file with Chinese characters and then I get the following error in Wc_DownloadFileCompleted: "The remote server returned an error: (404) Not Found.". I have tried HttpUtility.UrlEncode to encode the URL but that gives me an error on the Uri constructor or if I just encode the last part I get the same 404 error.

This is the URL giving me the problems:

http://example.com/Uploads/-463941/480630/1802+201830030210+孟万青.CNC.cloudfile

I have double-checked that the file is at that location and with the same filename.

private void DownloadCloudFile(string url)
{
    WebClient wc = new WebClient();
    wc.DownloadFileCompleted += Wc_DownloadFileCompleted;
    string tmpfile = Path.GetTempFileName();
    wc.DownloadFileAsync(new Uri(url), tmpfile, tmpfile);
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Electron8
  • 43
  • 3
  • Urls can [only contain ASCII characters](https://stackoverflow.com/a/1916747/2791540). If you need to pass other characters, you can use [percent encoding](https://stackoverflow.com/a/2742985/2791540) – John Wu May 11 '20 at 18:30
  • Is there a reason you're using [the overload of `DownloadFileAsync` that takes three parameters](https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfileasync?view=netframework-4.8#System_Net_WebClient_DownloadFileAsync_System_Uri_System_String_System_Object_)? The third parameter is a `userToken`, not a file name. – Heretic Monkey May 11 '20 at 18:33
  • I guess you need to try [this way](http://blog.softartisans.com/2013/06/03/how-to-download-files-containing-special-or-non-ascii-characters-in-the-filename-using-asp-net/?utm_source=twitter&utm_medium=social&utm_content=274626), hope it will allow to download file. – Prakash May 11 '20 at 18:37

2 Answers2

0

HttpClient can download the file

private static async Task DownloadCloudFile(string url)
{
    string tmpfile = Path.GetTempFileName();
    using (HttpClient client = new HttpClient())
    {
        using (HttpResponseMessage response = await client.GetAsync(url))
        using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
        using (var fileStream = File.Create(tmpfile))
        {
            streamToReadFrom.Seek(0, SeekOrigin.Begin);
            streamToReadFrom.CopyTo(fileStream);
            fileStream.Close();
        }
    }
}

It look like a bug in WebClient.

The proper way to encode Unicode characters in URL is to convert them to UTF-8 and then percent-encode them, as described here.

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
0

Thanks for the comments. I could still not get it to work and decided on another route. I simply force the user to type in ASCII characters when naming the files.

Electron8
  • 43
  • 3