简体   繁体   English

FTP上传图片错误

[英]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 我正在尝试将图像上传到由虚拟主机托管的ftp服务器上,以存储用户个人资料图像,以便他们在关闭并重新打开我的应用程序时

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 App.exe中发生了'System.Net.WebException'类型的未处理异常。其他信息:远程服务器返回错误:(500)语法错误,命令无法识别

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. 某人对我说的一句话是“该错误非常普遍。这可能意味着您有防火墙或某些东西挡住了东西,或者这意味着服务器上不支持SSL” 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 进行主要问题...我的代码-(FTP部分)在公共静态课中

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! 我自己会给予我100%的帮助!

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: 上传到ftp的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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