amazon ec2 - Cannot make new directories in FTP (C#) -
i trying upload file ftp server (ec2) via simple c# windows application made.
problem i'm having : if directory exists, can make file inside it. however, if directory doesn't exist, cannot make directory.
what checked : double checked write permissions. logged in via winscp , can make new directories in there without problems. i've given user privileges.
my code :
private async void fileftpupload(string sourcefilepath, string folderpath) { string ftpurl = @"ftp://xx.xxx.xxx.xxx/" + "trial/12/1.txt"; console.writeline("ftp url : " + ftpurl); string ftpusername = "myusername"; string ftppassword = "mypassword"; try { string filename = path.getfilename(sourcefilepath); string ftpfullpath = ftpurl; webrequest ftp = webrequest.create(ftpurl); ftp.method = webrequestmethods.ftp.makedirectory; ftp.credentials = new networkcredential(ftpusername, ftppassword); ftp.method = webrequestmethods.ftp.uploadfile; filestream fs = file.openread(sourcefilepath); byte[] buffer = new byte[fs.length]; fs.read(buffer, 0, buffer.length); fs.close(); stream ftpstream = ftp.getrequeststream(); ftpstream.write(buffer, 0, buffer.length); ftpstream.close(); } catch (exception ex) { console.writeline("exception occurred : " + ex); //throw ex; } } now, if "trial/12/" exists, 1.txt made. otherwise, error :
system.net.webexception: remote server returned error: (553) file name not allowed.
i'm not sure what's happening , what's not. use assistance.
thank you.
edit : when : string ftpurl = @"ftp://xx.xxx.xxx.xxx/" + "trial";
it doesn't make directory. makes file named "trial".
doing :
string ftpurl = @"ftp://xx.xxx.xxx.xxx/" + "trial/"; gives same error 553
you have seperate code 2 requests. first should create directory , second upload file.
var filepath = @"ftp://xx.xxx.xxx.xxx/trial/12/1.txt"; var credentials = new networkcredential(ftpusername, ftppassword); { // create directory var directorypath = path.getdirectoryname(filepath); var request = webrequest.create(directorypath); request.method = webrequestmethods.ftp.makedirectory; request.credentials = credentials; using(var response = (ftpwebresponse)requestdir.getresponse()) { // todo: handle errors } } { // upload file using (webclient client = new webclient()) { client.credentials = credentials; client.uploadfile(filepath, "stor", sourcefilepath); } }
Comments
Post a Comment