简体   繁体   中英

FTP Upload image error

I am trying to upload images to my ftp server hosted by a web hosting to store users profile images for when they close and re open my application

Note If there is any other way I can store it please suggest it

I have tryed the following code below but I keep receiving a error saying

An unhandled exception of type 'System.Net.WebException' occurred in App.exe Additional information: The remote server returned an error: (500) Syntax error, command unrecognized

A comment a person said to me was "That error is quite generic. It could mean you have a firewall or something blocking something or it can mean that SSL is not supported on the server" Could any of you help towards this comment. Because I don't understand how i can block the firewall or stop it or excreter excreter (Not that important help if you can)

Carrying on to the main problem ... My code - (FTP Part) In a public static class

public static void UpLoadImage(string source)
    {
        try
        {
            String sourcefilepath = source;
            String ftpurl = "ftp://www.locu.site90.com/public_html/"; 
            String ftpusername = "a4670620";
            String ftppassword = "********";
            string filename = Path.GetFileName(source);
            string ftpfullpath = ftpurl + filename;
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
            ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);

            ftp.KeepAlive = false;
            ftp.EnableSsl = 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;
        }
    }

And here is where I call the void when selecting a image

private void button1_Click(object sender, EventArgs e)
    {
        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);
        }
    }

Any help given is 100% appreciated from me myself!

I am using the following code to create the path:

System.Net.FtpWebRequest ftpReq = null;
System.Net.FtpWebResponse ftpRes = null;
try
{
ftpReq = System.Net.WebRequest.Create(path) as System.Net.FtpWebRequest;
ftpReq.Credentials = new System.Net.NetworkCredential(user, password);
ftpReq.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory;
ftpReq.KeepAlive = false;
ftpRes = ftpReq.GetResponse() as System.Net.FtpWebResponse;
ftpRes.Close();
}
catch (WebException we)
{
   //do something with the error
}
catch (Exception e)
{   
   //do something with the error
}

and to upload the file why you don't use

public byte[] UploadFile(string address, string fileName):

Code for uploading to ftp:

WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(tabFolder.FldUser, tabFolder.FldPassword);
wc.UploadFile(folder.TrimEnd(@"\".ToCharArray()) + @"\" + fn, jpgFile);

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