Is there any way to get the file extension from a URL

前端 未结 7 1703
再見小時候
再見小時候 2021-02-12 07:04

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

The file will not be at URLs like:

h         


        
7条回答
  •  心在旅途
    2021-02-12 07:34

    It is weird, but it works:

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

    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);
    

    result: ".jpg?par=x"

提交回复
热议问题