FTP上传图片错误

时间:2014-02-26 08:13:50

标签: c# ftp

我正在尝试将图像上传到我的ftp服务器,该服务器由一个虚拟主机托管,用于存储用户个人资料图片,以便他们关闭并重新打开我的应用程序

注意如果还有其他方式可以存储,请提出建议

我在下面尝试了以下代码,但我一直收到错误说

App.exe中发生未处理的“System.Net.WebException”类型异常附加信息:远程服务器返回错误:(500)语法错误,命令无法识别

一个人对我说的评论是“这个错误非常通用。这可能意味着你有防火墙或阻止某些东西,或者它可能意味着服务器上不支持SSL”可能你们任何人都有助于这个评论。因为我不明白我是如何阻止防火墙或阻止防火墙或排泄分泌物(如果可以的话,不是那么重要的帮助)

继续主要问题...我的代码 - (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;
        }
    }

这是我在选择图像时称之为空白的地方

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);
        }
    }

我自己100%赞赏任何帮助!

1 个答案:

答案 0 :(得分:0)

我使用以下代码创建路径:

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
}

并上传文件,为什么不使用

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

上传到ftp的代码:

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