I am trying to build a list of all files on a ftp server.
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(remote);
request.Credentials = new NetworkCredential(userName, passWord);
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> files = new List<string>();
// Build List of all files
string line = streamReader.ReadLine();
while (!string.IsNullOrWhiteSpace(line))
{
files.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();
Now, the problem is, whe i use this list with the broken/incomplete file to download all the files with a Webclient, the code breaks when trying to download the incomplete file...
The remote server returned an error: (550) File unavailable (e.g.,
file not found, no access)
How can i skip these files when building the list?
I the catch of the exeption i just added a "ignore" when the exeptions was "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)"
This way i dont get the error and the file gets downloaded just fine when its complete on the ftp server.
Related
I have been working on an azure function. I am trying to access FTP from the function at every 3 seconds of interval. Sometimes, I get FTP error 550 while I try to access it.
Here is the code
try
{
FtpWebRequest ftpRequest = GetFTPWebRequest(WebRequestMethods.Ftp.ListDirectory, path);
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
return streamReader;
}
catch (Exception ex)
{
}
The frequency of the error is higher sometimes.
What can possibly be the issue here. As the error isn't traced at FTP sometimes.
Can We have some preventive steps here.?
I would like to download a file from a FTP and directly POST to a REST URL without having to save in a temp folder, which I am currently doing. Works great, but not what I want to do. When I take a file directly from the FTP, the File.OpenRead takes not only the FTP path
ftp://xxxx.xxxxxx.xxx/
but it adds the project path
C://myProject/ProjectName
in front of it, thus causing a
The filename, directory name, or volume label syntax is incorrect.
Where am I going wrong?
using(var client = new WebClient())
{
client.Headers.Add("Content-Type", "application/octet-stream");
using(var filestream = File.OpenRead(FTP_Path + FTP_filename))
using(var requestStream = client.OpenWrite(new Uri(fileUploadUrl), "POST"))
{
filestream.CopyTo(requestStream);
}
}
We recently switched our ftp site at work from ftp://address to https://ftp.address and now my application that retrieves files is unable to download files with expected content.
I previously used FtpWebRequest and it has been working for many years.
I am now trying to use WebClient and it downloads a file but it is not the text file I need. Instead, the contents of the file turns out to be some HTML.
Previous that worked:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(getAppSetting("URI") + getAppSetting("FilePath") + args[0].ToString());
request.Method = WebRequestMethods.Ftp.DownloadFile;
setCredentials(request);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Changed code to handle https:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
WebClient request = new WebClient();
setCredentials(request);
string file = request.DownloadString(getAppSetting("URI") + getAppSetting("FilePath") + args[0].ToString());
The result is a file that is downloaded but it contains HTML. I expect to have the contents of the file on the website I am pulling from.
After a couple of weeks of research, I finally found out what I needed to do and the answer was quite simple. I found out that our new website uses SSH so I installed SSH.Net via my nugent package to Visual Studio, and added "using Renci.SshNet;". I used the following code to download the file to my local drive:
using (SftpClient sftp = new SftpClient("ftp.website.com", 22,"username", "password")
{
sftp.Connect();
if (sftp.IsConnected)
{
using (Filestream fs = new FileStream("Output.txt", FileMode.Create))
{
sftp.DownloadFile("/file/Client/Input.txt", fs);
}
}
sftp.Disconnect();
}
This works perfectly. My file no longer downloads as an HTML file and I get my desired output. I hope this saves someone else a lot of time.
I have made some code that connects to an FTP server.
My problem is that I against some servers get both folder and filename, e.g. myfolder\myfile.txt, and others just get myfile.txt.
var request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/myfolder");
request.Method = WebRequestMethods.Ftp.ListDirectory;
var ftpResponse = (FtpWebResponse) request.GetResponse();
var ftpResponeStream = ftpResponse.GetResponseStream();
var ftpStreamReader = new StreamReader(ftpResponeStream);
string line;
while ((line = ftpStreamReader.ReadLine()) != null)
{
Console.WriteLine(line);
}
I would prefer that I just get myfile.txt because that is how the real code should run, but I don't know, if this is a FileZilla setting or something else.
The URL for the ListDirectory method should end with a slash, in general.
Without a slash, results tend to be uncertain, largely depending on the FTP server implementation.
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/myfolder/");
With a URL like ftp://ftp.example.com/parent/folder, without the slash, the FtpWebRequest does:
CWD /parent
NLST folder
In this case, some FTP servers include the folder in the listing, while some do not.
While with a slash, the FtpWebRequest does:
CWD /parent/folder
NLST
In this case, the listing includes bare filenames.
I am trying to create an SSIS package that gets a folder tree structure from a remote SQL DB and recreates that structure in a document library in Sharepoint.
I tried to (hard) code a Script Task to create just one folder but I'm getting this error:
http://img856.imageshack.us/img856/1386/helpel.png
After running only a part of this script:
Microsoft.Sharepoint.Client.dll
Microsoft.Sharepoint.Client.Runtime.dll
public void Main()
{
ClientContext clientContext = new ClientContext("http://spdemo.example.com/");
clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "SPDEMO");
Web rootWeb = clientContext.Web;
Dts.TaskResult = (int)ScriptResults.Success;
}
I've scoured the internet for solutions, but haven't found something that works for me.
So basically i need to find out how to:
create a folder
populate it with sub-folders
Copy files in bitestreams from SQL to Sharepoint, all in SSIS
Thanks in advance!
Regards,
Vlad Ardelean
After some research I found out that in order to reference a DLL in a SSIS Script Task it first has to be strong named
Instead, I have found an easier way to create folders and upload files:
public void CreateFolder(string url)
{
HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(url);
request.Credentials = new NetworkCredential(user, pass);
request.Method = "MKCOL";
HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
response.Close();
}
public void UploadDocument(byte[] file, string destinationName)
{
byte[] buffer = file;
WebRequest request = WebRequest.Create(destinationName);
request.Credentials = new System.Net.NetworkCredential(user, pass);
request.Method = "PUT";
request.ContentLength = buffer.Length;
BinaryWriter writer = new BinaryWriter(request.GetRequestStream());
writer.Write(buffer, 0, buffer.Length);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
}
url : represents the url path of the folder you want to create
http://spsite.host.com/DocLib/FolderToCreate
destinationName: path + document name
http://spsite.host.com/DocLib/FolderToCreate/DocToCreate.docx
Did you try running your code from a stand-alone windows application written in C# in Visual Studio?
If I get errors in SSIS, my preference is to test that code thoroughly in Visual Studio and then port it to a script task in SSIS.
If you haven't done folder creation and file copy to SharePoint, the below links might help.
Create a folder - http://thingsthatshouldbeeasy.blogspot.in/2009/09/how-to-add-folder-to-sharepoint-list.html
You can do the create folder step recursively untill you have created the destination tree structure.
Copy Files - http://msdn.microsoft.com/en-us/library/ms454491.aspx, http://msdn.microsoft.com/en-us/library/ms470176.aspx
I dont understand what you mean by copy file in bitestreams from SQL. If you want to read binary data from SQLServer via ADO.Net use System.Data.SqlDbType.VarBinary
Read the data from SQL server to a stream and write to a temp file, copy that temp file from local to SP. Or you may even write the data in a stream to SP directly without a temp file.