What is the best way to URLEncode a file name in Delphi?

My desktop application should download the file from the Internet. The file path is known, the file name itself is half-variable, i.e. Someone will post new files there, and my application will have to download them.

Now I want to make sure that the URL is safe and that it will be interpreted correctly. Also, if the file name has "#" (you never know), I want it to be encoded. Javascript has two different functions for this: encodeURI and encodeURIComponent . The latter also encodes the characters "#", among other things. Of course, I could fold my own, but I decided that theres must be functions ready for this, and I could also avoid some old mistake.

I will upload the file using an object that uses a number of WinInet API functions ( InternetOpen , etc.).

So, I started rummaging through MSDN and, of course, theres UrlCanonicalize . But there is also UrlEscape , CreateUri (but this is not present in Delphi 2010 divisions) and, finally, InternetCreateUrl , in which I need to share the entire URL. Id rather combines the first part of the URL with the name URLEncoded filename.

In addition, all of them have many different flags, different default values ​​that changed during the versions of Windows, and I can no longer figure out the differences. Does anyone know which one is better for this purpose?

+4
source share
2 answers

try the TIdURI.PathEncode function (located in the idURI module), which is part of the Indy library included in delphi.

 {$APPTYPE CONSOLE} {$R *.res} uses idURI, SysUtils; Var FileName : string; Encoded : string; begin try FileName:='File with a Very weird f***name*#%*#%<>[]'; Encoded:=TIdURI.PathEncode(FileName); Writeln(Encoded); except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; Readln; end. 

It will return

 File%20with%20a%20Very%20weird%20f%2A%2A%2Aname%2A%23%25%2A%23%25%3C%3E%5B%5D 

You can also look in the functions TIdURI.URLDecode and TIdURI.URLEncode .

+8
source

Source: https://habr.com/ru/post/1401654/


All Articles