有没有办法从URL获取文件扩展名 [英] Is there any way to get the file extension from a URL

查看:71
本文介绍了有没有办法从URL获取文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,以确保将从脚本中下载的文件具有我想要的扩展名.

I want to know that for make sure that the file that will be download from my script will have the extension I want.

文件将不在以下网址:

http://example.com/this_url_will_download_a_file

也许是,但是,我认为我只会使用这种URL:

Or maybe yes, but, I think that I will only use that kind of URL:

http://example.com/file.jpg

我不会使用Url.Substring(Url.LastIndexOf(".") - 3, 3)进行检查,因为这是一种非常差的方法.

I will not check it with: Url.Substring(Url.LastIndexOf(".") - 3, 3) because this is a very poor way.

那么,您推荐我做什么?

So, what do you recommend me to do?

推荐答案

这很奇怪,但是有效:

string url = @"http://example.com/file.jpg";
string ext = System.IO.Path.GetExtension(url);
MessageBox.Show(this, ext);

但是如下面的crono所述,它不适用于参数:

but as crono remarked below, it will not work with parameters:

string url = @"http://example.com/file.jpg?par=x";
string ext = System.IO.Path.GetExtension(url);
MessageBox.Show(this, ext);

结果:.jpg?par = x"

result: ".jpg?par=x"

这篇关于有没有办法从URL获取文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆