Post: Retrieving the extension of a file
11-04-2015, 10:52 PM #1
jagex
Gym leader
(adsbygoogle = window.adsbygoogle || []).push({}); Useful if you want to download a file but not sure what the file extension is. If there are better ways to do this feel free to share, always looking to improve.
    
public static string GetFileExtension(string url)
{

try
{
WebRequest request = WebRequest.Create(url);

using (WebResponse response = request.GetResponse())
{
string fileExtension = response.Headers["Content-Type"];

switch (fileExtension)
{
case "image/jpeg":
return "jpg";
default:
return String.Empty;
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return String.Empty;
}
Last edited by jagex ; 11-04-2015 at 10:55 PM.
11-06-2015, 08:20 AM #2
Dan
I'm a god.
Originally posted by jagex View Post
Post


It seems it only grabs jpg images? Maybe change the title to that?
11-06-2015, 09:31 AM #3
Toxic
former staff
Originally posted by jagex View Post
Useful if you want to download a file but not sure what the file extension is. If there are better ways to do this feel free to share, always looking to improve.
    
public static string GetFileExtension(string url)
{

try
{
WebRequest request = WebRequest.Create(url);

using (WebResponse response = request.GetResponse())
{
string fileExtension = response.Headers["Content-Type"];

switch (fileExtension)
{
case "image/jpeg":
return "jpg";
default:
return String.Empty;
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return String.Empty;
}


it only returns if it's a .jpg image :|
11-07-2015, 12:38 AM #4
jagex
Gym leader
It can return anything. Its just an example.

If you want it to return other files then you need to check what the header is and add it. Like if you want mp4 videos, you would add

    
case "video/mp4"":
return "mp4";


text/plain Plain text. Default if data is primarily text and no other type detected.
text/html HTML. Default if common tags detected and server does not supply image/* type.
text/xml XML data. Default if data specifies <?xml with an unrecognized DTD.
text/richtext Rich Text Format (RTF).
text/scriptlet Windows script component.
audio/x-aiff Audio Interchange File, Macintosh.
audio/basic Audio file, UNIX.
audio/mid Windows Internet Explorer 7 and later. MIDI sequence.
audio/wav Pulse Code Modulation (PCM) Wave audio, Windows.
image/gif Graphics Interchange Format (GIF).
image/jpeg JPEG image.
image/pjpeg Default type for JPEG images.
image/png Internet Explorer 7 and later. Portable Network Graphics (PNG).
image/x-png Internet Explorer 7 and later. Default type for PNG images.
image/tiff Tagged Image File Format (TIFF) image.
image/bmp Bitmap (BMP) image.
image/x-xbitmap Removed from Windows Internet Explorer 8.
image/x-jg AOL Johnson-Grace compressed file.
image/x-emf Enhanced Metafile (EMF).
image/x-wmf Windows Metafile Format (WMF).
video/avi Audio-Video Interleaved (AVI) file.
video/mpeg MPEG stream file.
application/octet-stream Binary file. Default if data is primarily binary.
application/postscript PostScript (.ai, .eps, or .ps) file.
application/base64 Base64-encoded bytes.
application/macbinhex40 BinHex for Macintosh.
application/pdf Portable Document Format (PDF).
application/xml XML data. Must be server-supplied. See also "text/xml" type.
application/atom+xml Internet Explorer 7 and later. Atom Syndication Format feed.
application/rss+xml Internet Explorer 7 and later. Really Simple Syndication (RSS) feed.
application/x-compressed UNIX tar file, Gzipped.
application/x-zip-compressed Compressed archive file.
application/x-gzip-compressed Gzip compressed archive file.
application/java Java applet.
application/x-msdownload Executable (.exe or .dll) file.
Last edited by jagex ; 11-07-2015 at 12:42 AM.
11-07-2015, 07:08 AM #5
Dan
I'm a god.
Originally posted by jagex View Post
Post


I would suggest making it based off user input, instead of hard coding. Just a suggestion.

The following user thanked Dan for this useful post:

Bitwise
11-07-2015, 12:51 PM #6
Winter
Purple God
Originally posted by jagex View Post
Useful if you want to download a file but not sure what the file extension is. If there are better ways to do this feel free to share, always looking to improve.
    
public static string GetFileExtension(string url)
{

try
{
WebRequest request = WebRequest.Create(url);

using (WebResponse response = request.GetResponse())
{
string fileExtension = response.Headers["Content-Type"];

switch (fileExtension)
{
case "image/jpeg":
return "jpg";
default:
return String.Empty;
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return String.Empty;
}


The idea of the "Content-Type" header is so the developer can have an image type to display in our case "image" and the filetype "jpeg". So if we do this..

    
string getUrlExtension(string url) {
var request = HttpWebRequest.Create(url);
return request.GetResponse().Headers["Content-Type"].Split('/'Winky Winky[1];
}

getUrlExtension("https://www.somesite.com/download.filetype")
yields

filetype

We can get the type of file we'll be saving. If you really wanted to be fancy you could do something like this
    
struct contentType_t {
public string fileType;
public string friendlyName;
}

contentType_t getContentTypeForURL(string url) {
var response = HttpWebRequest.Create(url).GetResponse();
return new contentType_t() { friendlyName = response.Headers["Content-Type"].Split('/'Winky Winky[0], fileType = response.Headers["Content-Type"].Split('/'Winky Winky[1] };
}


which now we can do
    
var url = "https://www.somesite.com/download.filetype";
var content = getContentTypeForURL(url);
var sfd = new SaveFileDialog();
sfd.Filter = string.Format("{0}|*{1}", content.friendlyName, content.fileType);

if (sfd.ShowDialog() == DialogResult.OK) {
new WebClient().DownloadFile(url, sfd.FileName);
}


Last edited by Winter ; 11-07-2015 at 11:43 PM.
11-07-2015, 02:34 PM #7
jagex
Gym leader
Thanks winter

@Dan, its for a downloader im working on, there is no user input.
11-07-2015, 11:45 PM #8
Winter
Purple God
Originally posted by jagex View Post
Thanks winter

@Dan, its for a downloader im working on, there is no user input.


Jagex, I thought I might tell you unless you quote the person they probably wont know that you mentioned them because doing @USERNAME doesn't work Smile
11-08-2015, 01:07 AM #9
jagex
Gym leader
Haha, thanks xD

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo