C
C#2y ago
Kiel

Checking if an arbitrary (remote) URL is a file attachment, and extracting it if it is? [Answered]

If I'm given an arbitrary URL (I don't have control over it), how can I check if it appears to be a direct link to a file, and if it is, extract the filename + extension? It's been a little while so I don't remember why I stopped using it, but I tried using Path.GetFileName() and it didn't work correctly all of the time. IIRC, it would preserve any garbage data like query parameters after the end of the filename, so instead of http://example.com/image.png?width=420&height=69 producing image.png, it would produce image.png?width=420&height=69? It's been a hot second so I don't remember if this was the exact problem.
7 Replies
Kiel
Kiel2y ago
After asking this I realize that I could probably just still use Path.GetFileName() and then trim everything after the first ? after the last period? Still, dunno if there are better methods out there, I'm curious
333fred
333fred2y ago
Use a Uri to extract the path Then use Path.GetFileName after it's been normalized
Kiel
Kiel2y ago
So I construct a Uri with the url, and then use Path.GetFileName(uri.LocalPath)? or is it RemotePath?
333fred
333fred2y ago
Probably AbsolutePath?
For the URI file://computer/file.ext, the absolute path is /file.ext and the local path is \computer\file.ext.
Not sure which is the one you want
Kiel
Kiel2y ago
AbsolutePath works just fine 👍
Kiel
Kiel2y ago
now to check how it handles, well, a URI not being to a file with an extension Just in case anyone else needs to know this information: - if your arbitrary URL ends in /, extracting the filename via Path.GetFileName() will return an empty string - if your arbitrary URL does not end in /, extracting the filename will return whatever text was after the last / but before any query parameters (essentially a fake file with no extension - check for periods, i guess?)
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
How does assigning default values when creating an instance work? [Answered]I'm not sure how to ask this question. I also can't for the life of me remember what this style of iProcess.EnableRaisingEvents does not work on WindowsI have an app that have 20 workers that are other processes. On production, on ubuntu, when I close Top-level statements in MSVSSay you've created a new project with top-level statements in MSVS. Is it possible to change it to *how to format multiple conditions assignments in return statement [CLOSED] [Answered]Greetings, I was wondering, how could I properly format this piece of code. ```cs return res > PagEntity Framework One-To-One [Answered]Code at: https://paste.mod.gg/rccwzntafwoj/0 When attempting to change the database I'm getting: TDatabase - Relationship returns empty set even though records are present [Answered]I think I'm probably missing something stupid obvious... In my batch model, I have ```cs private reaSave storageFile to a folder on driveI have a storagefile ready to give to a control to crop an image the problem is that image crop wilDynamically load .dll into .NET 6 ProgramHi, I've a Blazor WebAssembly Hosted (.NET 6) web application. I've put some part of the applicationMove File To A Matching Type RefactoringWhen everything is top-level do you not need namespaces because everything is "top-level"? Thanks foClass making my program run as a background process after I close it [Answered]https://paste.myst.rs/7o8qz5vk After I close my program through RMB>Close window it continues runni