Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, June 23, 2013 10:24 AM
I have an FTP account on drivehq.com, and I'm trying to send all my PDF files from my desktop to it. But, because at each iteration ( i'm iterating through a string array, which contains the path file of the all PDF files from my desktop ) I'm creating a new FtpWebRequest, drivehq has a built-in antispam upload file, and they block my access for a while. Can somebody make this routine / script to use the FtpWebRequest only once, and to upload all PDF files?
// The PDFiles array ( desktopDir = My Desktop Directory )
List<string> PDFiles = Directory.GetFiles(desktopDir, "*.pdf", SearchOption.AllDirectories).Where(x => File.GetLastAccessTime(x).Date.ToShortDateString() == DateTime.Today.Date.ToShortDateString())
.Select(x => x)
.ToList();
// Upload the files
FtpWebRequest requestFTPUploader;
FileInfo fileInfo;
FileStream fileStream;
Stream uploadStream;
int bufferLength = 2048;
int contentLength;
for (int i = 0; i < PDFiles.Count; ++i)
{
requestFTPUploader = (FtpWebRequest)WebRequest.Create("ftp://ftp.drivehq.com/" + Path.GetFileName(PDFiles[i]));
requestFTPUploader.Credentials = new NetworkCredential("MyAccount", "MyPassword");
requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;
fileInfo = new FileInfo(PDFiles[i]);
fileStream = fileInfo.OpenRead();
byte[] buffer = new byte[bufferLength];
uploadStream = requestFTPUploader.GetRequestStream();
contentLength = fileStream.Read(buffer, 0, bufferLength);
while (contentLength != 0)
{
uploadStream.Write(buffer, 0, contentLength);
contentLength = fileStream.Read(buffer, 0, bufferLength);
}
uploadStream.Close();
fileStream.Close();
requestFTPUploader = null;
}
All replies (4)
Sunday, June 23, 2013 7:36 PM âś…Answered | 1 vote
You can try the approach that I have been using successfully from my code samples article.
UploadFiles/CopyFolderFiles method
This is designed to ftp all files in given folder, but you can modify it if you want it to work differently.
The key to making this work reliably on a bad network connection is to wait at least one additional second each time an error occurs. Then retry the upload.
Dan Randolph - My Code Samples List
Sunday, June 23, 2013 11:14 AM
Have you tried moving the creation/login (two lines) and disposal of the FtpWebRequest object to outside the loop?
Mike
Sunday, June 23, 2013 11:24 AM
Have you tried moving the creation/login (two lines) and disposal of the FtpWebRequest object to outside the loop?
Mike
I can't Mike, cause I have to put the name of the PDF file from the desktop
requestFTPUploader = (FtpWebRequest)WebRequest.Create("ftp://ftp.drivehq.com/" + Path.GetFileName(PDFiles[i]));=> Path.GetFileName(PDFiles[i])
Thursday, February 25, 2016 7:04 PM
The task is already achieved with a single line of code from the cmd prompt or .bat file by running the console app (WebClientFtp) written in C#:
WebClientFtp -source <path-to-local-folder> -ftpdest <uri-ftp-directory> [ -user <userName> -pwd <password> ]
Dan Randolph - My Code Samples List