0
votes

The following piece of code is used to read a file from server and download it to the client pc:

var webClient = new WebClient();
webClient.OpenReadCompleted += (s, e) =>
{
    using (var fs = (Stream) dialog.OpenFile())
    {
           e.Result.CopyTo(fs);
           fs.Flush();
           fs.Close();
    }
 };
 webClient.OpenReadAsync(GetFileUri(fileToDownload));

When downloading a .txt file everything is ok. But when I try to download a .dat file I get following exception: System.Reflection.TargetInvocationException with inner exception System.Net.WebException: The remote server returned an error: NotFound.The Uri is relative and doesn't present an issue with the .txt file.

I can't seem to find information if this should be possible or what the issue could be. The error itself also doesn't give me much. Any thoughts?

1
Well does the server definitely return the file for that URL? Does it work in a browser? - Jon Skeet
When I put the Url in the browser I get this: HTTP Error 404.3 - Not Found The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map. - LittleWhiteFairy
Right. So that's the problem - nothing to do with your client code. - Jon Skeet
".dat" is not a default MIME type. You have to configure your web server. - Jeroen Heier

1 Answers

0
votes

There was a setting missing in the config file. I needed to add the following:

<system.webServer>
  <staticContent>
    <mimeMap fileExtension=".dat" mimeType="application/octet-stream" />
  </staticContent>
</system.webServer>