|
-
Jul 8th, 2011, 09:58 AM
#1
Thread Starter
Frenzied Member
Uploading /creating directory FTP
I have written code which uploads a file to a server (this will eventually have to change to by FTP) it currently works correctly.
However I have since tried to modify it to create the directory for the file if it doesnt exist. This part is causing me problems.
FileWebResponse FTPRes = (FileWebResponse)FTPReq.GetResponse();
when the code hits the above line I get a file not found error? Its confusing as I dont expect it to find it I want it to create the directory so I can upload to it?
Code:
private static void Upload(string ftpServer, string userName, string password, string filename, string directory)
{
if (!WebRequestMethods.Ftp.ListDirectoryDetails.Contains(directory))
{
FileWebRequest FTPReq = FileWebRequest.Create(directory) as FileWebRequest;
FTPReq.Credentials = new NetworkCredential("", "");
FTPReq.Method = WebRequestMethods.Ftp.MakeDirectory;
FileWebResponse FTPRes = (FileWebResponse)FTPReq.GetResponse();
}
using (System.Net.WebClient client = new System.Net.WebClient())
{
client.Credentials = new System.Net.NetworkCredential(userName, password);
client.UploadFile(ftpServer + "/" + new FileInfo(filename).Name, "STOR", filename);
}
}
-
Jul 8th, 2011, 09:28 PM
#2
Re: Uploading /creating directory FTP
This here doesn't do anything useful:
Code:
if (!WebRequestMethods.Ftp.ListDirectoryDetails.Contains(directory))
I assume that what you're trying to do there is get a list of directories from the FTP server and see whether your 'directory' is one of them. That's not what that code is doing though. WebRequestMethods.Ftp.ListDirectoryDetails is not a method that returns a list of directory details. It's a constant that returns the String value "LIST". I think that it's safe to say that "LIST" will never contain whatever the value of 'directory' is.
I was going to write something else but then I noticed that you are creating a FileWebRequest and then assigning WebRequestMethods.Ftp.MakeDirectory to its Method property. You can't mix and match here. The FtpWebRequest.Method property expects a value from the WebRequestMethods.Ftp class and the FileWebRequest.Method property expects a value from the WebRequestMethods.File class. You're either working with the file system or an FTP server, not a bit of both.
With that in mind, the WebRequestMethod.File class has only UploadFile and DownloadFile fields. If you want to work with directories then use the System.IO.Directory class. Basically, you cannot write your code for the file system in a way that you can simply Find & Replace "File" with "Ftp" and have it work with an FTP server.
-
Jul 13th, 2011, 10:40 AM
#3
Thread Starter
Frenzied Member
Re: Uploading /creating directory FTP
Oh
In that case it is definately the FTP server I want to work with as it is a colsole app I am writing to upload files/directories to a an externally hosted location.
With this in mind I have changed my code to FTP as below.
Code:
if (!WebRequestMethods.Ftp.ListDirectoryDetails.Contains(directory))
{
FtpWebRequest FTPReq = FtpWebRequest.Create(directory) as FtpWebRequest;
FTPReq.Credentials = new NetworkCredential("", "");
FTPReq.Method = WebRequestMethods.Ftp.UploadFile;
FtpWebResponse FTPRes = (FtpWebResponse)FTPReq.GetResponse();
}
However I still have the problem of needing to create a director if it doesnt exist! also ive just noticed whilst the code im using requires user/ pass credentials it doesnt allow for the actual ftp address to be input, I dont understand how it could ever connect to the site I require?
-
Jul 13th, 2011, 08:42 PM
#4
Re: Uploading /creating directory FTP
OK, I said in my last post that this is wrong:
Code:
if (!WebRequestMethods.Ftp.ListDirectoryDetails.Contains(directory))
but you're still doing it. You do not use the WebRequestMethods class for anything other than getting a String value to assign to the Method property of a WebRequest. This is the correct usage:
Code:
FTPReq.Method = WebRequestMethods.Ftp.UploadFile;
If you want a list of directory details then you have to do that, i.e. create an FtpWebRequest and set its Method property to WebRequestMethods.Ftp.ListDirectoryDetails. The response will contain the directory details and you can check that to see if your directory of interest exists.
As for connecting and creating the folder, I've never done it myself but I believe that you have to connect to the parent folder and then write the new folder name to the request stream.
You may want to look at this for some help:
http://www.codeproject.com/KB/IP/Ftp...w=Quick&fr=201
-
Jul 14th, 2011, 09:37 AM
#5
Thread Starter
Frenzied Member
Re: Uploading /creating directory FTP
Ok so currently im knowwhere near the create directory if not exists stuff, im just trying to upload a file (which doesnt already exist)
but am getting the error
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
550 Uploadfolder/product/2781.pdf: Access is denied.
Code:
private void uploadFTP(String vendor, String filename)
{
//try
//{
String destinationPath = ConfigurationManager.AppSettings["HostedCourseOutlinesPDFPath"].ToString();
String sourcePath = ConfigurationManager.AppSettings["InternalCourseOutlinesPDFPath"].ToString();
String destinationFolder = destinationPath + "/" + vendor;
String sourceFolder = sourcePath + "\\" + vendor;
String uploadFile = sourceFolder + "\\" + filename;
const String username = "myuserid";
const String password = "mypassword";
String URI = destinationFolder + "/" + filename;
System.Net.FtpWebRequest ftp = FtpWebRequest.Create(URI) as FtpWebRequest;
ftp.Proxy = null;
//ftp.Credentials = new System.Net.NetworkCredential(username, password);
ftp.KeepAlive = false;
//ftp.UseBinary = true;
ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
using (System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)ftp.GetResponse())
{
using (System.IO.Stream responseStream = response.GetResponseStream())
{
//loop to read & write to file
using (System.IO.FileStream fs = new System.IO.FileStream(uploadFile, System.IO.FileMode.Create))
{
byte[] buffer = new byte[2047];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
} while (read != 0);
responseStream.Close();
fs.Flush();
fs.Close();
}
responseStream.Close();
}
response.Close();
}
//}
//catch(WebException e)
//{
// String status = ((FtpWebResponse)e.Response).StatusDescription;
//}
}
-
Jul 14th, 2011, 10:28 AM
#6
Thread Starter
Frenzied Member
Re: Uploading /creating directory FTP
That now works for uploading after I granted the account correct permissions,
Except - The file it uploads is of 0kb and has no content. If I try open the pdf it says it cant be opened as it is not a supported format or has been damaged.
Last edited by FishGuy; Jul 14th, 2011 at 10:33 AM.
-
Jul 14th, 2011, 11:38 AM
#7
Thread Starter
Frenzied Member
Re: Uploading /creating directory FTP
Changed code to this based on another thread which now uploads data.
Im just back to the create and directory issue now.
Code:
System.Net.FtpWebRequest ftp = FtpWebRequest.Create(URI) as FtpWebRequest;
ftp.Proxy = null;
//ftp.Credentials = new System.Net.NetworkCredential(username, password);
ftp.KeepAlive = false;
ftp.UseBinary = false;
ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
byte[] b = System.IO.File.ReadAllBytes(uploadFile);
System.IO.Stream clsStream = ftp.GetRequestStream();
clsStream.Write(b, 0, b.Length);
clsStream.Close();
clsStream.Dispose();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|