如何从URL获取文件名,C#/ .NET? [英] How Get File Name from URL ,C#/.NET?

查看:706
本文介绍了如何从URL获取文件名,C#/ .NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果地址以这样的方式结束,我该如何获取文件名。

/ download / file / 36fdd4aebda3819d329640ce75755657bc0c0d4c6811432b3bb6aac321dc78d /?

How can I get a file name if the address ends with something like this.
/download/file/36fdd4aebda3819d329640ce75755657bc0c0d4c6811432b3bb6aac321dc78d/ ?

这是简化的代码示例,

void geckoWebBrowser1_Navigating(object sender, GeckoNavigatingEventArgs e)
    {
             if (e.Uri.AbsolutePath.Contains("/file/"))
                {
                      MyUrl = e.Uri.ToString();
                         // and here I tried different codes construstors, methods but I can not get the filename
                }
            if (e.Uri.Segments[e.Uri.Segments.Length - 1].EndsWith(".exe"))
                {
                       // if the address ended with the FileName this code works well
                       Uri u = new Uri(e.Uri.ToString());
                      string filename = Path.GetFileName(ut.LocalPath);
                       MessageBox.Show(fileName);
                }
     }


推荐答案

没有文件名,除非有 Content-Disposition 标题,包括文件名参数它可能类似于:

It doesn't have a "file name", unless there's a Content-Disposition header, including a filename-parm. It might resemble:

Content-Disposition: attachment; filename=FileA.abc

在这种情况下,文件名将为 FileA .abc

In that case, the file name would be FileA.abc.

我没有真正使用 WebClient 类,但是,我没有看到任何简单的方式访问标题。您必须切换到使用 HttpWebRequest 和自己做更多的工作。

I've not really worked with the WebClient class, however, and I don't see any easy way to access the headers. You'd have to switch to using HttpWebRequest, and do more work yourself.

这篇关于如何从URL获取文件名,C#/ .NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆