- Home /
FTP Upload Files
Hello!
I am trying to set up a very simple app that records some audio from the cellphone's mic and sends the record to a FTP server. Everything is working fine, but the FTP connection. Any clues why is this happening? I read several forums but couldn't make it work. Does it have anything to do with the syntax? "/", ":", and so on...? I keep getting this error:
UriFormatException: Invalid URI: The format of the URI could not be determined: ftp.Smarterasp.nettest.png System.Uri..ctor (System.String uriString, Boolean dontEscape)
Many thanks in advance! Here is the code:
public string m_FtpHost = "ftp://ftp.ftpserver.net/site1/trial";
//public string filepath = "C:/Users/xxx/Desktop/24-02 09.32.32/test.png";
public string m_FtpUsername = "user-001";
public string m_FtpPassword = "xxx";
public string filepath;
public void UploadFile()
{
// Get an instance of WebClient
WebClient client = new System.Net.WebClient();
// parse the ftp host and file into a uri path for the upload
Uri uri = new Uri(m_FtpHost + new FileInfo(filepath).Name);
Debug.Log (uri);
// set the username and password for the FTP server
client.Credentials = new System.Net.NetworkCredential(m_FtpUsername, m_FtpPassword);
// upload the file asynchronously, non-blocking.
client.UploadFileAsync(uri, "STOR", filepath);
}
Answer by Pratap-Dafedar · Feb 25, 2016 at 06:41 AM
Try this code snippets given in following link,
/* Create Object Instance */
ftp ftpClient = new ftp(@"ftp://10.10.10.10/", "user", "password");
/* Upload a File */
ftpClient.upload("etc/test.txt", @"C:\Users\metastruct\Desktop\test.txt");
/* Download a File */
ftpClient.download("etc/test.txt", @"C:\Users\metastruct\Desktop\test.txt");
/* Delete a File */
ftpClient.delete("etc/test.txt");
/* Rename a File */
ftpClient.rename("etc/test.txt", "test2.txt");
/* Create a New Directory */
ftpClient.createDirectory("etc/test");
/* Get the Date/Time a File was Created */
string fileDateTime = ftpClient.getFileCreatedDateTime("etc/test.txt");
Console.WriteLine(fileDateTime);
/* Get the Size of a File */
string fileSize = ftpClient.getFileSize("etc/test.txt");
Console.WriteLine(fileSize);
/* Get Contents of a Directory (Names Only) */
string[] simpleDirectoryListing = ftpClient.directoryListDetailed("/etc");
for (int i = 0; i < simpleDirectoryListing.Count(); i++) { Console.WriteLine(simpleDirectoryListing[i]); }
/* Get Contents of a Directory with Detailed File/Directory Info */
string[] detailDirectoryListing = ftpClient.directoryListDetailed("/etc");
for (int i = 0; i < detailDirectoryListing.Count(); i++) { Console.WriteLine(detailDirectoryListing[i]); }
/* Release Resources */
ftpClient = null;
link here: http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class
Thank you Pratap Dafedar!
Everything work just fine. But you need to change string (#80) for upload
function in ftp
class. Old/wrong string:
FileStream localFileStream = new FileStream(localFile, File$$anonymous$$ode.Create);
New/working string:
FileStream localFileStream = new FileStream(localFile, File$$anonymous$$ode.Open);
In other case your file will be replaced with empty one.
Thank you @Pratap-Dafedar file is successfully upload to ftp server but it loss packet at server machine it shows zero bytes please help me for this .. thank you so much
Answer by Bunny83 · Feb 25, 2016 at 05:04 AM
Well, you're using a WebClient. A WebClient represents an HTTP client. FTP is an entirely different protocol. You need an actual FTP client. Something like FtpWebRequest which is part of the stock .NET classes or any other FTPClient implementation
Your answer
Follow this Question
Related Questions
Server closing after hide VDS window 0 Answers
Unity networking tutorial? 6 Answers
How connect client to server when ip is private ? 2 Answers