none
How to binding image url (ftp) from server to picture Edit (or pictureBox) in WindownForm

    Question

  • I am have a problem to get image from server (have link) to binding pictureEdit.
    a function Create PathFile ( I have path file (ftp://....))

    public string CreatePathFile(string fileName, string subFolder)
        {
            string localPath = string.Empty;
            try
            {
                System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                string ftp_host = configuration.AppSettings.Settings["FTP_Host"].Value.ToString();
                string ftp_homedir = configuration.AppSettings.Settings["FTP_HomeDir"].Value.ToString();
                string ftp_username = configuration.AppSettings.Settings["FTP_Username"].Value.ToString();
                string ftp_password = configuration.AppSettings.Settings["FTP_Password"].Value.ToString();
                string temp_location = System.IO.Path.GetTempPath();
    
                System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(temp_location);
                if (!System.IO.Directory.Exists(temp_location))
                {
                    System.IO.Directory.CreateDirectory(temp_location);
                }
    
                WebClient request = new WebClient();
                request.Credentials = new NetworkCredential(ftp_username.Normalize(), ftp_password.Normalize()); 
                string pathFile = "ftp://";
                if (String.IsNullOrEmpty(ftp_homedir))
                {
                    if (String.IsNullOrEmpty(subFolder))
                    {
                        pathFile += ftp_host;
                    }
                    else
                    {
                        pathFile += ftp_host + "/" + subFolder;
                    }
                }
                else
                {
                    if (String.IsNullOrEmpty(subFolder))
                    {
                        subFolder = ftp_homedir;
                    }
                    else
                    {
                        subFolder = ftp_homedir + "/" + subFolder;
                    }
                    pathFile += ftp_host + "/" + subFolder+ "/" + fileName;
                }
                localPath = pathFile;
            }
            catch (Exception ep)
            {
                MessageBox.Show("ERROR: " + ep.Message.ToString());
            }
            return localPath;
        }
    }

    and a function binding to pictureBox

    public void SetPictureProduct(string filename)
        {
            string link=CreatePathFile(filename, "Products");
            //have link: etc: ftp://119.79.35.120:990/med.template/TANMY_TEMPLATE/Products/0501010012.01.03.png
            if (!string.IsNullOrEmpty(link))
            {
                formPic.ImageLocation = link;
                //formPic: name of pictureBox
            }
        }

    Result: The remote server returned an error: (530) Not logged in.

    I can upload image to sever but I can't load from server to pictureEdit.

    Can you help me? Many Thanks. Dev 9.3.3


    • Edited by v-bui Thursday, February 08, 2018 2:02 AM
    • Moved by CoolDadTxMVP Friday, February 09, 2018 3:50 PM Winforms related
    Thursday, February 08, 2018 1:59 AM

All replies

  • Hello v-bui,

    Try to add username and password to your ftp url. The format like below.

    ftp://user:password@host:port/path

    The components obey the following rules:

    1. user: a user name (user id) on the host.
    2. password:  the password corresponding to the user name; note: if the user and password field in a URL contains character : or @ or /, the character must be encoded.
    3. host:  the fully qualified domain name of a network host, or its IP address.
    4. port:  the port number to connect to; it omitted, defaults to 21.
    5. path: path specification of the form cwd1 / cwd2 / ... / cwdN / name.

    Try the below url.

    ftp://username:password@119.79.35.120:990/med.template/TANMY_TEMPLATE/Products/0501010012.01.03.png

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 09, 2018 8:49 AM