get extension of file(edit) - c#

i want get extension file of url .
for example get extension http://www.iranfairco.com/download-file/02912bb889cb24.
if extension this file is gif.
EDIT:
for example this url "http://www.online-convert.com/result/d8b423c3cbc05000cc52ce7015873e72"
Please help me how can get extension of this url?

Why does Path.GetExtension not work for you?
EDIT
Ah, so your url doesn't specify the extension you are after. That means anything could be behind it.
Just by inspecting the string value of the url, you can't see what will behind it. Your (second) example point (ultimately) to a .gif file, but it could also have been a .jpg, .doc (or even .exe).
You will need to do a webrequest to see what you get back. Usually you could try a HEAD to get just the headers (and inspect the content type), but I don't think that will work here. Try Fiddler to see what you get back, then you can refine your question.

Check out here:
http://www.java2s.com/Code/CSharp/Network/GetHTTPResponseheaders.htm
You will be interested in the response.ContentType which should give you something like "image/gif" for a gif image

try this
string path = #"http://www.iranfairco.com/download-file/02912bb889cb24.gif";
string e = Path.GetExtension(path );

This URL appears to perhaps be linking to a page which will have a download for a file.
The 02912bb889cb24 is perhaps a database reference to that file.
Therefore without access to any API for this system, I don't think its possible at all to work out the file extension for the file accessed via this URL

You can just split the string on char '.' and get the last string in the returned array..

Related

Question Mark in Querystring with Server.MapPath returning an error

I am working on a web application. Here I am storing attachments/uploads on server physical directory. The parent folder of uploads may contain special characters like '?'
Example of URL
"~/ChapterFiles/Capgeminisdfsdf_BE CSE ?_CoverPic/CoverPic.jpg"
When I am doing, Server.MapPath() on this URL, I am getting an error "Illegal characters in path."
Can't remove question mark from folder name as it's part of requirement. Please suggest a solution, I need to fix it urgently.
You can use something like:
String absoluteDir = Server.MapPath("~");
String myRelativePath = "~/ChapterFiles/Capgeminisdfsdf_BE CSE ? _CoverPic/CoverPic.jpg".Replace("/","\\");
String absolutePath = Path.Combine(absoluteDir,myRelativePath);
It will work. I advice you to write some unit tests for this function.
Use HttpServerUtility.UrlEncode and UrlDecode to encode/decode the string.
Question marks are not allowed in folder names in Windows. Your requirement in it's current form is impossible to implement and there is no "fix". You need to rethink how to map URL's to folder and file names.
You need to use # sign before the string. Like below
#"~/ChapterFiles/Capgeminisdfsdf_BE CSE ?_CoverPic/CoverPic.jpg"
Reference link

URL getting Appended when using Response.Redirect

I have a URL say /Registration/GetName.aspx/?language=English
When i click on a Asp.net Button on the same Page and say Response.Redirect("CheckLoginName.aspx");
It gives me a weird URL
/Registration/GetName.aspx/CheckLoginName.aspx
What should i do
Please Help?
You should use "~/" inside your Redirect
So your code will look something like this
Response.Redirect("~/CheckLoginName.aspx");
Hope this helps
You should remove the trailing / before the query string, since it serves no purpose. Your URL should be /Registration/GetName.aspx?language=English. Another option is to have Response.Redirect("../CheckLoginName.aspx"); This should also work.
I think a solution using a relative path is better, since it is location independant. If you move these two files to another URL, there will be no need for code changes.

Passing parameter via quertystring

I want to pass 3 parameters to ashx file to handle the image, for that i used ImageUrl=ImageHandler.ashx?uid=1&iid=1&pid=1 but the image get not bound..
Help with correct syntax..
You may forget " for the code plus the correct location:
ImageUrl="~/ProjectLocationPathToTheImage/ImageHandler.ashx?uid=1&iid=1&pid=1"
If ImageUrl=ImageHandler.ashx?uid=1&iid=1&pid=1 is a part of your URL, then it appears that ImageUrl=ImageHandler.ashx is a query string parameter. If so, you need to change ?uid= to &uid=. What is the full URL that you are having issues with?

How to get filename from a file "template.docx.amp" in c#?

I have got a strange template whose extension is ".docx.amp" . Now i want to get fileName. i.e "template". Path.GetFileNameWithoutExtension() fails as it returns template.docx. Is there any other built in mechanism or i will have to go the ugle string comparison/split way. Please suggest and give some workaround
Nope, you will have to use some kind of string split, eg:
var nameWithoutExtension = filename.Split('.')[0];

Is there a more correct type for passing in the file path and file name to a method

What I mean by this question is, when you need to store or pass a URL around, using a string is probably a bad practice, and a better approach would be to use a URI type. However it is so easy to make complex things more complex and bloated.
So if I am going to be writing to a file on disk, do I pass it a string, as the file name and file path, or is there a better type that will be better suited to the requirement?
This code seems to be clunky, and error prone? I would also need to do a whole bit of checking if it is a valid file name, if the string contains data and the list goes on.
private void SaveFile(string fileNameAndPath)
{
//The normal stuff to save the file
}
A string is fine for the filename. The .Net Framework uses strings for filenames, that seems fine.
To validate the filename you could try and use a regular expression or check for invalid characters against System.IO.Path.GetInvalidFileNameChars. However, it is probably easier to handle the exceptional cases of invalid filenames by handling the exception that will occur when you try and create the file - plus you need to do this anyway....
Unfortunate as it is, string is the idiomatic way of doing this in .NET - if you look at things like FileStream constructors etc, they use strings.
You could consider using FileInfo (or DirectoryInfo) but that would be somewhat unusual.
You could use FileInfo (from System.IO) to pass it around, but strings are more or less standard when referring to files.
You could always use Path.GetFileName({yourstring}) to get the filename from the path.
String is fine, but you should put in some effort to ensure that the file is being saved into the directory you expect.
If you're working with a filepath a string is usual.
If you're working with a URL you could consider using the System.Uri class e.g.
Uri myUri = new Uri(myUrl, UriKind.Absolute);
This will allow you to work with properties such as uri.Host, uri.Absolute path etc. It will also give you a string array (Segments) for the separate subfolders in the url.
MSDN info here: System.Uri

Categories