Get original filename when downloading with WebClient

Viewed 27800

Is there any way to know the original name of a file you download using the WebClient when the Uri doesn't contain the name?

This happens for example in sites where the download originates from a dynamic page where the name isn't known beforehand.

Using my browser, the file gets the orrect name. But how can this be done using the WebClient? E.g.

        WebClient wc= new WebClient();
        var data=   wc.DownloadData(@"www.sometime.com\getfile?id=123");

Using DownloadFile() isn't a solution since this method needs a filename in advance.

4 Answers

To get the filename without downloading the file:

public string GetFilenameFromWebServer(string url)
{
    string result = "";

    var req = System.Net.WebRequest.Create(url);
    req.Method = "HEAD";
    using (System.Net.WebResponse resp = req.GetResponse())
    {
        // Try to extract the filename from the Content-Disposition header
        if (!string.IsNullOrEmpty(resp.Headers["Content-Disposition"]))
        {
            result = resp.Headers["Content-Disposition"].Substring(resp.Headers["Content-Disposition"].IndexOf("filename=") + 9).Replace("\"", "");
        }
    }

    return result;
}

If you, like me, have to deal with a Content-Disposition header that is not formatted correctly or cannot be parsed automatically by the ContentDisposition class for some reason, here's my solution :

string fileName = null;

// Getting file name
var request = WebRequest.Create(url);
request.Method = "HEAD";

using (var response = request.GetResponse())
{
    // Headers are not correct... So we need to parse manually
    var contentDisposition = response.Headers["Content-Disposition"];

    // We delete everything up to and including 'Filename="'
    var fileNameMarker= "filename=\"";
    var beginIndex = contentDisposition.ToLower().IndexOf(fileNameMarker);
    contentDisposition = contentDisposition.Substring(beginIndex + fileNameMarker.Length);

    //We only get the string until the next double quote
    var fileNameLength = contentDisposition.ToLower().IndexOf("\"");
    fileName = contentDisposition.Substring(0, fileNameLength);
}
Related