简体   繁体   中英

FTP Upload image

I was wondering what is wrong with this code? Im hosting a ftp on 000webhost and i want to upload a image that the user on my program opens from there computer using the openfiledialog feature

The Button to open image:

        OpenFileDialog open = new OpenFileDialog();
        if (open.ShowDialog() == DialogResult.OK)
        {
            Bitmap bit = new Bitmap(open.FileName);
            pictureBox1.Image = bit;
            pictureBox2.Image = bit;
            bit.Dispose();
            string fullPath = open.FileName;
            string fileName = open.SafeFileName;
            string path = fullPath.Replace(fileName, "");
            User.Details.UpLoadImage(fullPath);
        }

The code to upload it:

try
        {
            String sourcefilepath = source; // e.g. “d:/test.docx”
            String ftpurl = "ftp://www.locu.site90.com/public_html/"; // e.g. ftp://serverip/foldername/foldername
            String ftpusername = "********"; // e.g. username
            String ftppassword = "********"; // e.g. password
            string filename = Path.GetFileName(source);
            string ftpfullpath = ftpurl;
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
            ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);

            ftp.KeepAlive = true;
            ftp.UseBinary = true;
            ftp.Method = WebRequestMethods.Ftp.UploadFile;

            FileStream fs = File.OpenRead(source);
            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)
        {
            throw ex;
        }
    }

I keep getting some errrors "The requested URI is invalid for this FTP command" and the second error is "The remote server returned an error: (530) Not logged in .”

Since you are doing an upload. The destination file name is required in the FTP url. It looks like that is what you might of intended to do with the following line:

string ftpfullpath = ftpurl;

Try changing it to:

string ftpfullpath = ftpurl + filename;

For the not logged in error, some hosting companies only allow secure connections. You can try adding the following line to your code:

ftp.EnableSsl = true;

I am using this method, and that is working well:

public static void UpLoadImage(string image, string targeturl)
        {
            FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://www.website.com/images/" + targeturl);
            req.UseBinary = true;
            req.Method = WebRequestMethods.Ftp.UploadFile;
            req.Credentials = new NetworkCredential("user", "pass");
            byte[] fileData = File.ReadAllBytes(image);
            req.ContentLength = fileData.Length;
            Stream reqStream = req.GetRequestStream();
            reqStream.Write(fileData, 0, fileData.Length);
            reqStream.Close();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM