Convert UNC path to 'file:///' URL in ASP.NET - c#

I need to convert UNC paths to file:/// URLs. For example:
\\fileserver\share\dir\some file.ext --> file://///fileserver/share/dir/some%20file.ext
Is there a built-in function for this?

Yes, use the Uri class in the System namespace:
Uri uri = new Uri(#"\\fileserver\share\dir\some file.ext");
string url = uri.AbsoluteUri;

Related

How to download a file to the default storage location using HTTP Response from a given URL

I am new to C# and reactjs and am stuck in this conundrum. I am using a function to download the images from url into my local file. Doing that I am specifying the file path.
The function is this:
[HttpPost]
[Route("GetImages")]
public IHttpActionResult GetImages()
{
Somewhere();
using (WebClient webClient = new WebClient())
{
var url = "https://firebasestorage.googleapis.com/v0/b/tsl-coil-qlty-
monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926?
alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4";
webClient.DownloadFile(url, #"D:\CQMS_Images_JPEG\image4.jpg");
}
return Ok();
}
The problem is I am specifying a file path in the function. Can it be made so that I do not need to specify the file path and it will get downloaded to a default location.
Please help also please let me know if this can be done with HTTP Response?
The file path that I am giving here is "D:\CQMS_Images_JPEG\image4.jpg"
There are multiple ways to solve this issue -
You can create an entry inside configuration section in web config that will have location information and use that value inside DownloadFile function instead of hard code path.
You can create a folder inside your application and use the Server.MapPath to get that location. e.g - You have folder name abc then you will get exact path by -

C# - Create new URI object to point to a Local file

How can I create a URL object that points to a file that is Local to my web application I thought this would work:
var myURI = new Uri("~/Content/css/pdfX.css");
but I am getting this error:
Invalid URI: The format of the URI could not be determined.
Thanks
You need to convert the virtual path that you have to the corresponding physical file path. You do that with the Server.MapPath method.
var myURI = new Uri(Server.MapPath("~/Content/css/pdfX.css"));

How to open a local html file using NavigationWindow when path contains URL fragment?

I am using NavigationWindow to display html files, it worked fine until path does not contain any # or illegal characters.
I just wanted to know is there a way to open html file when the path contains URL fragment, for example: an anchor # like below..
"c:\MyFile.Html#tips"
Currently i am getting the following exception...
Could not find file 'c:\MyFile.Html#tips'
System.Net.WebException was unhandled
HResult=-2146233079
The problem is with the creation of the URI, it seems there is some bug with the URI class.
The URI generated by the following are different,
var filePath = #"c:\MyFile.Html#tips";
var uri = new Uri(filePath);
var uri2 = new Uri("file://" + filePath);
Fragment part of the uri is empty, but uri2 has correct fragment part as "#tips".
The same kind of problem is with the query even after creating URI as uri2, the reported bug for query
string can be found here, Why doesn't System.Uri recognize query parameter for local file path? and Uri class does not parse filesystem URL with query string

C# decodes URL containing %2F on path, is there any way to instruct API to send the URL as it is?

I am having a URL in below format
abcd.com/xyz/pqr%2Fss/abc
I want this to be send to server as it is.
When I build Uri using System.Uri it converts it to abcd.com/xyz/pqr/ss/abc
and it fails as I don't have a URL with the specified path.
When I tried with double encoding
(abcd.com/xyz/pqr%252Fss/abc) it send the Uri as it is but it fails as server side it is converted to (abcd.com/xyz/pqr%2Fss/abc)
If you construct your uri as such:
Uri u = new Uri("http://abcd.com/xyz/pqr%2Fss/abc")
Access the encoded string like this:
u.OriginalString
I had this problem too, but I found the solution: when you use HttpUtility.UrlEncode to be sure that the application will read the url right you have to construct the link this way:
http://www.abcd.com/xyz?val=pqr%2Fss
and not like this
http://www.abcd.com/xyz/pqr%2Fss
where pqr%2Fss is the result of the HttpUtility.UrlEncode("SOME STRING")

Why doesn't FTPWebRequest, or WebRequest in general accept a /../ path?

I am trying to automate some upload/download tasks from an ftp web server. When I connect to the server through client, or through Firefox even, in order to get to my directory, I have to specify a path like this:
ftp://ftpserver.com/../AB00000/incoming/files
If I try to access this:
ftp://ftpserver.com/AB00000/incoming/files
The server throws an error that the directory does not exist. So, the problem:
I am trying to create an FTPWebRequest with the first ftp address, but it always parses out the "/../" part and then my server says the path doesn't exist.
I've tried these:
Uri target = new Uri("ftp://ftpserver.com/../AB00000/incoming/files");
FtpWebRequest request = (FtpWebRequest)WebReqeuest.Create(target);
and
string target = "ftp://ftpserver.com/../AB00000/incoming/files";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
In the first bit, the path is already incorrect when the Uri object is instantiated, in the second bit, it's after the WebRequest.Create method. Any ideas what's going on?
EDIT:
Additionally, since I posted this, I have tried creating the URI with the no parse option. I have also tried something like this:
string ftpserver = "ftp://ftpserver.com/../";
string path = "12345/01/01/file.toupload";
Uri = new Uri(ftpserver, path, true);
And it always parses out the root part ("/../").
Try escaping the .. with something like:
Uri target = new Uri("ftp://ftpserver.com/%2E%2E/AB00000/incoming/files");
That works according to this blog which I found in this discussion.
Not really sure about it, but it may be for security reasons, since allowing "/../" URIs would potentially let people navigate freely on any server's file system.
Also, the official URI RFC states that when resolving an URI one of the steps performed is actually the removal of "/../" segments, so it's not a problem in the C# library but it's regular URI behavior.
Have you tried using the # symbol like so?
Uri target = new Uri(#"ftp://ftpserver.com/../AB00000/incoming/files");
FtpWebRequest request = (FtpWebRequest)WebReqeuest.Create(target);

Categories