Can FtpWebRequest get a file above the root folder? [duplicate] - c#

Can someone tell me how to change directories using FtpWebRequest? This seems like it should be an easy thing to do, but I'm not seeing it.
EDIT
I just want to add...I don't have my heart set on FtpWebRequest. If there's a better (easier) way to do FTP in .NET please let me know.
Apparently there's no way to do it using a live connection, you need to change the uri to trick ftpwebrequest into using a different request (thanks Jon).
So I'm looking for a 3rd party client...
Some of the open source solutions I tried didn't work too well (kept crashing), but I found one open source solution that's passed some of my preliminary tests (.NET FTP Client).

There's a blog post from Mariya Atanasova which shows how you can fake it - basically you have to put the directory on the URL.
I suspect you may be better off with a dedicated FTP library though - one that doesn't try to force everything into the WebRequest way of doing things. I haven't personally used any 3rd party libraries for this, but a search for "FTP library .NET" finds lots of candidates.
Edit: jcolebrand (in case of 2006 blog linkrot possibility)
Many customers ask us how they can use the CWD command with our FtpWebRequest.
The answer is: you cannot use the command directly, but you can modify the uri parameter to achieve the same result.
Let's say you're using the following format:
String uri = "ftp://myFtpUserName:myFtpUserPassword#myFtpUrl";
FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(uri);
Request.Method = "LIST";
The above example will bring you to your user's directory and list all the contents there. Now let's say you want to go 2 directories backwards and list the contents there (provided your user has permissions to do that). You close the previous FtpWebRequest and issue a new one with this uri
uri = "ftp://myFtpUserName:myFtpUserPassword#myFtpUrl/%2E%2E/%2E%2E";
This is equivalent to logging in with your user's credentials and then using cd ../../
Note: if you try using the ”..” directly without escaping them the uri class will strip them, so "ftp://myFtpUserName:myFtpUserPassword#myFtpUrl/../.." is equivalent to "ftp://myFtpUserName:myFtpUserPassword#myFtpUrl/"
Now let's say you want to go to another user's directory which is one level above the root. If you don't specify a user name and password it's equivalent to logging in as anonymous user. Then you issue a new FtpWebRequest with the following uri
"ftp://myFtpUrl/%2F/anotherUserDir"
This is equivalent to logging in as anonymous and then doing
Cd /
cd anotherUserDirectory

You have to close the current connection:
request.Close();
And open a new one with an other uri:
uri = "ftp://example.com/%2F/directory" //Go to a forward directory (cd directory)
uri = "ftp://example.com/%2E%2E" //Go to the previously directory (cd ../)
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);

Instead of using ListDirectory method of FTPWebRequest you can use ListDirectoryDetails method of FtpWebRequest .
From That you can use regular expression to get value you want.
Thats it,It work fine for me in my case

Related

Downloading from Sharepoint Document Library

Goal: Download documents from a Sharepoint 2010 Document Library using C#
I've searched and found many questions/resources regarding how to download documents to a local disk from a Sharepoint Document Library. I've so far been unable to successfully implement a solution.
For the sake of discussion, my document library is located at the following link:
http://server.domain.com/sites/CompanyLocation/dprtmnts/DepartmentName/MyLibraryName/Forms/AllItems.aspx
Within this document library are two folders, each containing a number of documents. I desire to retrieve a list of these documents (in each folder) and have the ability to download/open a document on command.
Things I've tried:
The linked post from this comment: https://sharepoint.stackexchange.com/a/105923
The linked post (https://stackoverflow.com/a/21056425/2480558) offers a solution for both downloading and uploading. I only need to download, so I tried that. This solution gives me a Microsoft.SharePoint.Client.ServerObjectNullReferenceException. There are no instructions on what the url, listTitle, or listItemId need to be, so I'm probably doing something wrong... it doesn't appear to be getting any files.
I also tried the first blog post on that answer that points to using a package from bendsoft. Apparently that package is a paid package, and it's quite expensive.. I can't go that route.
Another answer: https://stackoverflow.com/a/53733630/2480558
This gives me the following exception: (actual server edited out of exception)
Microsoft.SharePoint.Client.ClientRequestException: 'The IDCRL response header from server 'http://server.domain.com/' is not valid. The response header value is 'NTLM'. The response status code is 'Unauthorized'.
This solution: https://stackoverflow.com/a/15602003/2480558
Honestly, I don't even remember why that one didn't work.
If anyone has something that might help me get on the right path... I'm not familiar with making web requests and web authorization, or anything of that nature.
If you use the example that you found at https://stackoverflow.com/a/53733630/2480558, but you have to adapt it for your local SharePoint server, since that code is logging in to SharePoint Online.
In the public void Connect() definition, replace the line
clientContext.Credentials = new SharePointOnlineCredentials(UserName, securePassword);
with this instead:
clientContext.Credentials = new NetworkCredential(UserName, securePassword, "putYourADDomainNameHere");

How to rename a file/folder on OneDrive using Patch-Request?

I am about to write an App which synchronizes my local folder with the cloud. As far as I know the LiveSDK doesn't provide any method that would help me with that?
So after searching on the internet i found an example here : http://onedrive.github.io/items/move.htm
It is about moving a file, but there is also a name property which should changeable.
So how do I build the Request in C# ?
This is how I tried so far, do not really know how to build the URL, with what parameters and so on. Also, can i make a PATCH-Request with a WebClient?
string url = String.Format("https://apis.live.net/v5.0/" + fileid + "?access_token="+this.liveConnectClient.Session.AccessToken);
using (WebClient wc = new WebClient())
{
//wc.DownloadData(url);
wc.UploadData(url, "PATCH", null);
}
I would be grateful for any clues.
It doesn't look like WebClient can support PATCH requests. You'll likely need to use the HttpWebRequest method (https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.method.aspx) which supports additional verbs. The console on http://dev.onedrive.com should give you a better understanding of how the requests are put together for different calls.

How do I download documents from AtTask?

I'm working on a continuing API project. The current issue at hand is to be able to download my data from the AtTask server in precisely the folder structure they exist in on the AtTask servers. I've got the folder creation working nicely; the data types between Document, Document Folder and Document Version seem to be pretty clear. I am a little disillusioned about the fact that extension isn't in the document object (that I have to refer to the document VERSION for that)... but I can see some of the reason for that from a design perspective.
The issue I'm running into now is that I need to get the file content. I originally through from the API documentation that I'd be able to get to the file contents the same way as the documentation recommends uploading it -- through the handle. Unfortunately, neither document nor docv seem to support me accessing the handle except to write a new file.
So that leaves me the "download URL" as the remaining option. If I build the UI strings from the API calls using my browser, I get a URL with https://attaskURL/document/download?ID=xxxx (and can also get the versionID and such). If I paste the url into the browser where I'm logged in to the user interface of AtTask, it works fine and I can download the file. If, instead, I use my C# code to do so, I get the login page returned as a stream for me to download instead of my actual file because I'm not authenicated. I've tried creating a network credential and attaching it to the request with the username and password, but to no avail.
I imagine there's a couple ways to solve this problem -- the easy one being finding a way to "log in" to the download site through code (which doesn't seem to be the usual network credential object in C#) OR find a way to access the file contents through the API.
Appreciate your thoughts!
It looks like you can use the download URL if you put a session id in the URL. The details on getting a session id are here (basically just call login and a session id is returned in JSON):
http://developers.attask.com/api-docs/#Authentication
Then cram it on the end of your document download URL:
https://yourcompany.attask-ondemand.com/document/download?ID=xxxx&sessionID=abc1234
I've given this a quick test and I'm able to access a document.
You can use the downloadURL and a sessionID IF you are not using SAML authentication.
I have tried it both ways and using SAML will redirect you to the login page.

how to get directory files name from web

i want to read web directory files.
when i use this code.
string[] files;
string webfilepath = "http://www.anydomain.com/templates/images";
files = Directory.GetFiles(webfilepath, #"*.*", SearchOption.TopDirectoryOnly);
code shows error url format not support.is there any other way to read web directory.
thanks in advance.
is there any other way to read web directory
No. HTTP has no "directory index" you can retrieve, unless the server (or software running on it) generates it itself, for example Apache's Options +Indexes configuration. But then that index is generated in HTML, which you'll have to parse to get the full filename.
you cant do this in a generic way. Because its up to the web server how it dump the directory content on to client if it does that at all.
First you need to make sure that your server send out the content using some protocol. then you can use HttpWebRequest to send a HTTP request and get the result. you will have to do your own parsing on the result at the end of the day.
You are trying to pass url to Directory.GetFiles instead of passing physical path of folder, You can use Server.MapPath to get the physical path of url if it is accessible i.e. the code for accessing folder is running on the machine url is point to. If url is on different machine then you can not use Directory.GetFiles.

Using forward slashes ('/') in URI templates without encoding

I've got a RESTful WCF service that acts as a file store. Since there can be any number of directories and sub-directories, I'm trying to let the users access them by simply putting the file path into the URL. Is there a way I can do this without requiring the user to encode the slashes?
For example, what I want is a URI template of Files/{path} that can be accessed like http://localhost:8000/Files/folder1/subfolder2/subfolder3/file.jpg.
You can put a * at the end of your uri template. Inside your operation you can interpret the rest of the uri that matches to * as your file path.
you'll need to use URL rewriting techniques to accomplish this. if using .net 4 use Route table.
I'm afraid not understanding right. Would the following work?
Let the user enter something like folder1/subfolder2/subfolder3/file.jpg in path variable.
Perform: String encodedPath = path.Replace("/", "%2F");

Categories

Resources