9

I am looking for a solution for this problem for days and I can't find any.

I want to download a file from a webserver with a webclient. The download works fine, but I can't get the real filename, which is very important for me.

I read on many homepages, that the filename should be saved in the Content-Disposition-Header. Unfortunately this header of the site is empty. I tried to get it with:

string header_contentDisposition ="";
using (WebClient client = new WebClient())
            {
                client.OpenRead(link);

                header_contentDisposition = client.ResponseHeaders["Content-Disposition"];
                MessageBox.Show(header_contentDisposition);
            }

There is no information saved inside this header.

If I try to download the file with my Browser (IE, Opera, Chrome) the filename gets shown in the filedialog, so it has to be saved somewhere.

Can you imagine where I can find it?

EDIT: I can't extract it from the URL, because the link is generated by php like

http://www.example.com/download.php?id=10
Kenster
  • 23,465
  • 21
  • 80
  • 106
user1854270
  • 93
  • 1
  • 1
  • 4
  • 1
    Can you dump the headers which *are* present? Or perhaps browsers are defaulting it from the URL? – Jon Skeet Nov 26 '12 at 18:24
  • All headers I got: Age = 292161 Connection=keep-alive Accept-Ranges=bytes Content-Length=15953830 Cache-Control=max-age=31536000 Content-Type=application/vnd.android.package-archive Date=Fri, 23 Nov 2012 09:21:40 GMT Expires=Sat, 23 Nov 2013 09:21:40 GMT Last-Modified=Fri, 23 Nov 2012 08:38:35 GMT Server=nginx – user1854270 Nov 26 '12 at 18:35
  • Can you give us a real URL so we can reproduce this? – Jon Skeet Nov 26 '12 at 18:36
  • for example: http://upload.p-kratzer.com/index.php?dir=&file=GE.JPG of course, in this particular case i could get the filename from the "file" parameter, but I have to download from different servers and often that's not possible – user1854270 Nov 26 '12 at 18:40
  • Rather than giving an example which *isn't* representative of the problem, why don't you give us one which *is*? i.e. a URL where: 1) The filename isn't in the URL; 2) The filename isn't in the Content-Dispoition; 3) Browsers still manage to download the file correctly. – Jon Skeet Nov 26 '12 at 18:41
  • here it is:http://upload.p-kratzer.com/index.php?dir=&file=asdfasdfwervdcvvedb (the real file name should be "wetter.JPG") – user1854270 Nov 26 '12 at 18:52
  • @user1854270: That's just a redirect. – Jon Skeet Nov 26 '12 at 19:01
  • What I would do is use the debugger, and see what the content of your response is, and see if anything that looks like a filename is in there – Sam I am says Reinstate Monica Nov 26 '12 at 19:01

1 Answers1

18

You can try this:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        try
        {

            HttpWebResponse res = (HttpWebResponse)request.GetResponse();
            using (Stream rstream = res.GetResponseStream())
            {
                string fileName = res.Headers["Content-Disposition"] != null ?
                    res.Headers["Content-Disposition"].Replace("attachment; filename=", "").Replace("\"", "") :
                    res.Headers["Location"] != null ? Path.GetFileName(res.Headers["Location"]) : 
                    Path.GetFileName(url).Contains('?') || Path.GetFileName(url).Contains('=') ?
                    Path.GetFileName(res.ResponseUri.ToString()) : defaultFileName;
            }
            res.Close();
        }
        catch { }

Tested this on http://upload.p-kratzer.com/index.php?dir=&file=asdfasdfwervdcvvedb and it does return wetter.JPG.

Lunyx
  • 3,164
  • 6
  • 30
  • 46
  • Great Lunyx. Thank you. But when i use this for google drive links get a big result string. For example "attachment;filename=filename.apk;filename*=UTF-8''filename.apk" – smedasn Mar 09 '18 at 11:24
  • @smedasn Probably the space between `attachment;` and `filename=` is missing in the header Google drive supplies. It's not required by the standard, so you should handle that case. – Tom Lint Aug 07 '18 at 12:15