如何从 URL 获取文件名,C#/.NET?

发布于 2024-12-02 13:34:27 字数 894 浏览 2 评论 0原文

如果地址以这样的结尾,我怎样才能获得文件名。
/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);
                }
     }

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

This is the simplified code example,

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);
                }
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无风消散 2024-12-09 13:34:27

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

Content-Disposition: attachment; filename=FileA.abc

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


我还没有真正使用过 WebClient 类,但是,我没有看到任何简单的方法来访问标头。您必须切换到使用 HttpWebRequest,并且自己做更多的工作。

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

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


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.

与之呼应 2024-12-09 13:34:27

Content-Disposition 应该与

Content-Disposition: attachment; filename=\"abc.msi\""

您想要获取 abc.msi 的位置相同。为此,您可以使用以下代码。

            string contentDisposition = resp.Headers["Content-Disposition"];
            string fileName = string.Empty;
            if (!string.IsNullOrEmpty(contentDisposition))
            {
                string lookFor = "filename=";
                int index = contentDisposition.IndexOf(lookFor, StringComparison.CurrentCultureIgnoreCase);
                if (index >= 0)
                    fileName = contentDisposition.Substring(index + lookFor.Length).Replace("\"", ""); ;
            } 

Content-Disposition should be as

Content-Disposition: attachment; filename=\"abc.msi\""

in here you want to get abc.msi. for that you can use following code.

            string contentDisposition = resp.Headers["Content-Disposition"];
            string fileName = string.Empty;
            if (!string.IsNullOrEmpty(contentDisposition))
            {
                string lookFor = "filename=";
                int index = contentDisposition.IndexOf(lookFor, StringComparison.CurrentCultureIgnoreCase);
                if (index >= 0)
                    fileName = contentDisposition.Substring(index + lookFor.Length).Replace("\"", ""); ;
            } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文