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 work开发者_JAVA百科s well
Uri u = new Uri(e.Uri.ToString());
string filename = Path.GetFileName(ut.LocalPath);
MessageBox.Show(fileName);
}
}
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.
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("\"", ""); ;
}
精彩评论