如何从 URL 获取文件名,C#/.NET?
如果地址以这样的结尾,我怎样才能获得文件名。/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它没有“文件名”,除非有 Content-Disposition 标头,包括文件名参数。它可能类似于:
在这种情况下,文件名将是
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:
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.
Content-Disposition 应该与
您想要获取 abc.msi 的位置相同。为此,您可以使用以下代码。
Content-Disposition should be as
in here you want to get abc.msi. for that you can use following code.