1

I am trying to download a file using system.net.webclient.downloadfile method in asp.net core. I am facing an issue for a specific file name.

Name of the file is

There is something to download #12344.pdf

It is throwing me an error file name "There is something to download" can not find.

The issue is characters after # is not recognized by the DownloadFile method.

Example Code:

string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner #1234.gif", myStringWebResource = null;

// Create a new WebClient instance.
      WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
      myStringWebResource = remoteUri + fileName;
// Download the Web resource and save it into the current filesystem folder.
      myWebClient.DownloadFile(myStringWebResource, fileName);

any suggestions would be helpful

Community
  • 1
  • 1
gary
  • 140
  • 4
  • 19
  • 3
    `#12344.gif` is a fragment identifier and is usually not processed by the server if I remember correctly. If you need to specify this as part of the url sent to the server, you should escape it. `#xxx` is most commonly used to make the browser scroll to some specific portion of a site after downloading it. Replace the `#` character with `%23` and see if that doesn't fix it, ie' string fileName = "ms-banner %231234.gif", ...` – Lasse V. Karlsen Jun 27 '19 at 20:58
  • You could also use `HttpUtility.UrlEncode(fileName)` if you fear you might have other characters with similar problems. – Lasse V. Karlsen Jun 27 '19 at 21:02
  • Who gives files such terrible names? – Sach Jun 27 '19 at 21:17

0 Answers0