I am using Background Tranfer API to download stuff. So my query is pretty simple how can I get the file type such as HTML or MP4.
-
Please share with us what do you download and how in code. – pijemcolu Jun 27 '17 at 13:08
-
1By checking the file extension? – Jun 27 '17 at 13:18
-
What?? I am actually fetching the download from web, so it could be of any type. I can't check file extension because when starting a download I have to provide the file extension. – Jai Sharma Jun 27 '17 at 14:01
-
That last sentence makes no sense. You have the file extension. Why can't you check it? – Jun 27 '17 at 14:06
-
If you're just dealing with raw bytes and no file extension or content type then you can try checking the bytes as most binary formats have a signature in the first few bytes. For text like HTML you'd have to read enough to be satisfied, like finding a html tag at the beginning of the file. – juharr Jun 27 '17 at 15:01
-
No I don't have the file extension. What I meant was that I have to pass the file extension when starting a download. And a file downloaded from web could literally be of any type(html, txt, zip etc), which I can't manually give in. – Jai Sharma Jun 27 '17 at 15:57
-
I'm confused. You said you "have to provide the file extension" when starting a download. So how are you starting the download if you don't have the extension? – Jun 27 '17 at 16:02
-
It's okay, let's say user just got a fresh URL. Now when user pastes the URL in my app. In the Backend what I am doing is initializing a new download operation. So a download operation requires two things, one which user already provided, URL. Other is by what name you want to save with. Now here is the tricky part, sometimes links are nice such as "https://www.anything.com/index.html". Here I can retrieve the information from link by help of some regular expression. But lot of times link are not that nice. I hope you get it. – Jai Sharma Jun 27 '17 at 16:53
1 Answers
Put aside background transfer APIs, I think the first question actually you should know is "how to get the file extension from a download Uri".
For this,we need to consider several scenarios about the "Uri".
The download Uri does have a file extension followed, for example:
https://code.msdn.microsoft.com/windowsapps/Background-File-Downloader-a9946bc9/file/145559/1/BackgroundDownloader.zip
. In this case, we can usePath.GetExtension
method that can get the file extension directly.The download Uri has a file extension but also query parameters followed, for example:
https://i.stack.imgur.com/7e3M5.jpg?s=328&g=1
. In this case, after getting the extension byPath.GetExtension
, we need to get the actual extension by getting a sub string or other expressions.The download Uri doesn't contain a file extension. For example,
https://channel9.msdn.com/Events/Build/2017/T6056/captions?f=webvtt&l=en
. In this case, commonly we may get the MIME type from the content type of http response content header, and then mapping the corresponding file extension.
Here is a very simple demo I tested on my side for getting the file extensions of the above Uri
:
private async void btnuri_Click(object sender, RoutedEventArgs e)
{
string ext;
ext = await GetFileExtention("https://i.stack.imgur.com/7e3M5.jpg?s=328&g=1");
System.Diagnostics.Debug.WriteLine(ext);
ext = await GetFileExtention("https://channel9.msdn.com/Events/Build/2017/T6056/captions?f=webvtt&l=en");
System.Diagnostics.Debug.WriteLine(ext);
ext = await GetFileExtention("https://code.msdn.microsoft.com/windowsapps/Background-File-Downloader-a9946bc9/file/145559/1/BackgroundDownloader.zip");
System.Diagnostics.Debug.WriteLine(ext);
}
public async Task<String> GetFileExtention(string url)
{
string ext = "";
try
{
if (Path.HasExtension(url))
{
ext = Path.GetExtension(url);
ext = ext.Contains('?') || ext.Contains('=') ? ext.Substring(0, ext.LastIndexOf("?")) : ext;
}
else
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(new Uri(url));
IHttpContent res = response.Content;
string ContentType = res.Headers["Content-Type"];
string MimeType = ContentType.Substring(0, ContentType.LastIndexOf(";"));
switch (MimeType)
{
case "text/plain":
ext = ".txt"; break;
case "text/vtt":
ext = ".vtt"; break;
case "text/html":
ext = "html"; break;
default:
ext = ".unknown"; break;
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
return ext;
}
And something we need to pay attention about the demo:
If you are using background transfer, you need to get the
Content-Type
header from theWindows.Networking.BackgroundTransfer.ResponseInformation
object, not theIHttpContent
. For example:private async Task HandleDownloadAsync(DownloadOperation download, bool start) { ... ResponseInformation response = download.GetResponseInformation(); var contenttype = response.Headers["Content-Type"]; }
We list three scenarios about download Uri, but we are not sure if you have other scenarios that you should have other ways to deal with them.
- The file extension results are not guaranteed since the
Content-Type
and the file suffix are provided by the server. - We don't list all the MIME type mapping, the above just for simple testing. For more details and more MIME type mappings you can reference this thread and this package.

- 10,509
- 1
- 10
- 21
-
Great explanation. But why am I getting "null" contentType. And no, the request does have content-type(how do I know, making request using a website). – Jai Sharma Jun 28 '17 at 14:01
-
Can you help? As soon as I am adding "var contenttype = response.Headers["Content-Type"]; " it's throwing error the given key was not present in the dictionary. Why is it? – Jai Sharma Jun 28 '17 at 17:03
-
@JaiSharma, as I mentioned, it is not guaranteed since the content-type is provided by the server. Debug it and try yourself to found if there're any useful information provided in the header. For example, if the header contains Content-Disposition that provide the file extension. Otherwise please provide your Uri link for testing. – Sunteen Wu Jun 29 '17 at 01:12
-
Well there is a great website that makes https requests. I was using that and in the response header content-type does show. So this conforms that there is content-type header present. By the way I have been using google.com as sample url. And one more thing can I some how get all the headers present instead of providing a key for each. That would be handy. – Jai Sharma Jun 29 '17 at 10:15