C# get file name from URL - c#

How can I download file name from URL ?
Like if I have URL like http://localhost/?downloadFile=56 and server will return file example.png. Because when I try to use
WebClient wc = new WebClient();
wc.DownloadFileAsync(url, "{FILE-NAME}");
I having problem to get the file name automatically.

Browsers will use the contents of the filename parameter of the Content-Disposition header as the default filename. If such a header is not available, browsers will typically use a generated filename based on the final component of the URL's path component.
See some additional information here: http://blogs.msdn.com/b/ieinternals/archive/2010/06/07/content-disposition-attachment-and-international-unicode-characters.aspx

I'd think you'd have to download the file and then get the file name from the downloaded file. Not sure how this is a programming question though.

Related

How to get original file name of a file from url

I have uploaded my file to my website with a random name. Now I want to get the original file name.
I have tried to get the file name:
var dllUrl = "https://cdn.example.com/c6244c971f.dll";
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(dllUrl);
Console.WriteLine(myFileVersionInfo.OriginalFilename);
but it did not work.
How can I get the original name from the version resource?
The parameter of FileVersionInfo.GetVersionInfo is a file name, not a URL.
You need to download the file (see e.g. here) to a temporary file and then use FileVersionInfo.GetVersionInfo(temporaryFile).
There is no other was to do this, because the original file name is not encoded in the URL nor is there a HTTP verb (or any other standardized way) to get only the version resource of a file.
I agree with Klaus Gütter. You need to download the file.
Here is the code:
var uri = "https://cdn.example.com/c6244c971f.dll";
var uniqueFileName = Path.GetRandomFileName();
var uniqueFilePath = Path.Combine(#"D:\temp", uniqueFileName);
// Download the file
new WebClient().DownloadFile(uri, uniqueFilePath);
// Get file version info
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(uniqueFilePath);
Console.WriteLine(myFileVersionInfo.OriginalFilename);
// Delete the file from local disk
File.Delete(uniqueFilePath);

how to download pdf with name that i set in my code by Restlet Client

How to download pdf with name that i set in my code by Restlet Client?
RS is one Google plug-in for API Test Tool
The question is that I want to download PDF with the name that I set in my code ,but when I click download link in response body ,the PDF's name become to controller's name.
If RS can't do that, any other test API tool can do it ?
Please confirm file name has been set up correctly.just like this
string fileName = \\' + 'filename' + \\';
response.Content.Headers.ContentDisposition.FileName = fileName;

Read a valid formated html local file using chrome with tmp extension

I've got a local file with a temporary extension (.tmp) which contains valid HTML code. I would like to allow chrome to read this file as a basic HTML file.
When i load this file using chrome (e.g: file:///C:/file.tmp), chrome read it as a text file.
I'm using currently list of chromium command: http://peter.sh/experiments/chromium-command-line-switches/ but cannot find which command (if exists?) could let me do what i wish.
This question is not relative to csharp but i'm using a net application to load the file, here is the code:
var url = localFilePathUri; //e.g: file:///C:/file.tmp
string args = "--kiosk --allow-file-access " + url;
var test = System.Diagnostics.Process.Start("chrome.exe", args);
I could rename local file to use .html as extension but i would like to know if a more direct way exists?
Thank you!

C# Absolute URI removes ".." from URL

I have to upload a file via FTP to ftp://ftp.remoteServer.com
My root directory on remoteServer contains an "upload" and a "download" folder. I need to put my file in the "upload" directory. But on log in, the server automatically puts me in the "download" folder.
I tried doing this:
string serverTarget = "ftp://ftp.remoteServer.com/";
serverTarget += "../upload/myfile.txt";
Uri target = new Uri(serverTarget);
FTPWebRequest ftp = (FTPWebRequest)FtpWebRequest.Create(target);
using(Stream requestStream = ftp.GetRequestStream()) {
// Do upload here
}
This code fails with: (550) File unavailable (e.g., file not found, no access)
I debugged the code, and target.AbsoluteUri returns as ftp://ftp.remoteServer.com/upload instead of ftp://ftp.remoteServer.com/../upload (missing the ..)
If I put ftp://ftp.remoteServer.com/../upload in a browser, I can log in and verify this is the correct place where I want to put my file.
How can I get the FTPWebRequest to go to the correct place?
I believe you can encode the dots as %2E to keep the dots in your URI.
So something like:
string serverTarget = "ftp://ftp.remoteServer.com/%2E%2E/upload/myfile.txt";
Try this:
string serverTarget = "../upload/myfile.txt";
Uri uri = new Uri(serverTarget, UriKind.Relative);
Andy Evans' comment is correct.
Consider the URI: http://ftp.myserver.com/../. The .. means, "take me to the parent of this directory". But there is no parent! So when you derive the absolute URL, you're going to end up with http://ftp.myserver.com/ There is nothing else that the parser can do.
I think the problem is with the configuration of your FTP server. I assume that the directory structure looks something like:
ftproot
upload
download
It looks like the FTP service is automatically logging you to /ftproot/download. That is, the URI ftp.myserver.com gets mapped to /ftproot/download on the local file system. If that's the case, no amount of fiddling with the URI is going to get you anywhere. If the URI root is mapped to the download directory, there is no way you can, using the .. syntax, "go up one level and then down."
Are you able to upload using an FTP client such as Filezilla, or perhaps the Windows FTP command line tool? If so, what are the steps you take to do it? Can you make your code do the same thing?

Get version info of a patch file in c#

I am uploading a .msi file using fileupload control to a central location. Now i need to get version info of this file. I am using the following code.
FileVersionInfo patchFile = FileVersionInfo.GetVersionInfo(completeFilePath)
completeFilePath is the full path of the uploaded file. This code breaks and throws file not found exception.however, if i look down in the physical directory,file exists there.
Am i missing something or will i have to download this uploaded file again to some temp location and then extract version info from this file.
Second option i had was to get version info before uploading the file. In this case i am not able to get complete path of this patch file as fileupload control just gives the fileName and not the complete location.
Please suggest how to proceed.
I think the problem is in how to define "completeFilePath"
Remember that if the completeFilePath is a non-literal string then you must escape the special characters.
For example: [string filePath = "C:\\Windows\\FolderName\\FileName.txt";]
(notice the escape character ()
Another option is to use the literal string which enables you to use the special characters without having to use the escape character. An example is:
[string filePath = #""C:\Windows\FolderName\FileName.txt"";]
If this still doesn't work then could you please post how you are inputting this?

Categories

Resources