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

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

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 -

Open custom URI which is registered in Windows 8.1

i like to "open" a (local) customized URI witch is already registered in Windows 8.1.
I can open it, if the Link is embedded in a HTML Mail.
<a href="cobra://goto/addresses/ID=5545">
and it works in Windows Explorer:
cobra://goto/addresses/ID=5545
The ID is sent to the Cobra Application.
I tried to send this URI to the Cobra Application with a WebRequest / FileWebRequest / HttpWebRequest without any Success.
string uriToLaunch = #"cobra://goto/addresses/ID=" + ID;
Uri uri = new Uri(uriToLaunch);
FileWebRequest WebReq = (FileWebRequest)WebRequest.Create(uri);
WebReq.GetResponse();
Error: System.NotSupportedException: "URI prefix is not recognized."
Second try, after I recognized that the URI work with Windows Explorer:
File.Open(uriToLaunch, FileMode.Open);
Error: System.NotSupportedException: "The given path's format is not supported."
Any Suggestions how to do it? I donĀ“t want to build my own URI Parser because Windows knows how to handle it.
you can run a custom URI from C# by calling Process.Start()
eg
System.Diagnostics.Process.Start("uri://here");
The exception is occurring because your uri string is not formatted correctly (you have two prefixes - url: and cobra:)
Try the following:
string uriToLaunch = #"cobra://goto/addresses/ID=" + ID;

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

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

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;

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