Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
hey all, I want to select the items (file) which choose from treeview after that i click button upload, it file will be up to server. I get fail and erorr below:

UriFormatException was unhandle Invalid URI: The hostname could not be parsed.

Picture demo
http://i1055.photobucket.com/albums/s505/vn_photo/23.jpg[^]

Code of function upload
C#
private void UploadFile(string filename)
{
    FileInfo fileInfo = new FileInfo(filename);
    string FileAndFolder = "ftp://" + serverIP + "/" + fileInfo.Name;
    FtpWebRequest RequestFTP;
    RequestFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + serverIP + "/" + fileInfo.Name));
    RequestFTP.Credentials = new NetworkCredential(username, passwd);
    RequestFTP.KeepAlive = false;
    RequestFTP.Method = WebRequestMethods.Ftp.UploadFile;
    RequestFTP.UseBinary = true;
    int sizedata = 1024;
    byte[] data = new byte[sizedata];
    int contlenght;
    FileStream fs = fileInfo.OpenRead();

    try
    {
        Stream strm = RequestFTP.GetRequestStream();
        contlenght = fs.Read(data, 0, sizedata);
        while (contlenght != 0)
        {
            strm.Write(data, 0, contlenght);
            contlenght = fs.Read(data, 0, sizedata);
        }
        strm.Close();
        fs.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Upload Fail");
    }
}

Code of event click button upload
C#
private void btnUpload_Click(object sender, EventArgs e)
{
    String fileName = trwFileExplorer.SelectedNode.ToString();
    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("C:\\");
    System.IO.FileInfo[] files = dir.GetFiles();
    System.Collections.ArrayList arrList = new System.Collections.ArrayList();
    foreach (System.IO.FileInfo f in files)
    {
        fileName = System.IO.Path.GetFullPath(f.Name);
    }
    MessageBox.Show(fileName);

        UploadFile(fileName);
}
Posted
Updated 13-Oct-12 18:23pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900