【问题标题】:image corrupted while FTP upload using C#使用 C# 进行 FTP 上传时图像损坏
【发布时间】:2015-12-21 20:05:11
【问题描述】:

我正在尝试使用 C# 将 jpeg 上传到 ftp 服务器。文件已上传,但在打开时已损坏。 这是我的代码:

    /// <summary>
    /// Upload HttpPostedFileBase to ftp server
    /// </summary>
    /// <param name="image">type of HttpPostedFileBase</param>
    /// <param name="targetpath">folders path in ftp server</param>
    /// <param name="source">jpeg image name</param>
    public static void UploadFileToFTP(HttpPostedFileBase image,string targetpath,string source)
    {

        string ftpurl = ConfigurationManager.AppSettings["Ftp_Images_Domain"];
        string ftpusername = ConfigurationManager.AppSettings["Ftp_Images_Usr"];
        string ftppassword = ConfigurationManager.AppSettings["Ftp_Images_Pwd"]; 

        try
        {
            SetMethodRequiresCWD();
            string filename = Path.GetFileName(source);
            string ftpfullpath = ftpurl + targetpath + source;
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
            ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);

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


            HttpPostedFileBase myFile = image;
            int nFileLen = myFile.ContentLength;
            byte[] myData = new byte[nFileLen];


            Stream ftpstream = ftp.GetRequestStream();
            ftpstream.Write(myData, 0, nFileLen);
            ftpstream.Close();
            ftpstream.Flush();
        }
        catch (WebException e)
        {
            String status = ((FtpWebResponse)e.Response).StatusDescription;
        }
    }

我假设问题出在我写入 ftp 流的字节数组中。只是想不出如何解决它。 任何帮助表示赞赏:-) 谢谢

【问题讨论】:

    标签: c# ftp httppostedfilebase


    【解决方案1】:

    byte[] myData 永远不会使用图像数据进行初始化。下面是正确的代码。我从代码中删除了一些不需要的行。试试看,让我知道它是否工作正常

    /// <summary>
    /// Upload HttpPostedFileBase to ftp server
    /// </summary>
    /// <param name="image">type of HttpPostedFileBase</param>
    /// <param name="targetpath">folders path in ftp server</param>
    /// <param name="source">jpeg image name</param>
    public static void UploadFileToFTP(HttpPostedFileBase image,string targetpath,string source) {
    
        string ftpurl = ConfigurationManager.AppSettings["Ftp_Images_Domain"];
        string ftpusername = ConfigurationManager.AppSettings["Ftp_Images_Usr"];
        string ftppassword = ConfigurationManager.AppSettings["Ftp_Images_Pwd"]; 
    
        try {
            SetMethodRequiresCWD();
    
            string ftpfullpath = ftpurl + targetpath + source;
    
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
            ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
            ftp.KeepAlive = false;
            ftp.UseBinary = true;
            ftp.Method = WebRequestMethods.Ftp.UploadFile;
    
            using ( Stream ftpstream = ftp.GetRequestStream() )
                image.InputStream.CopyTo(ftpstream)
        } catch (WebException e) {
            String status = ((FtpWebResponse)e.Response).StatusDescription;
        }
    }
    

    【讨论】:

    • 我今天遇到了这个问题,当我读到 CopyTo 方法时,我想“当然...”谢谢。您还可以通过在using ... 之前添加image.Seek(0, SeekOrigin.Begin); 来避免零长度文件
    【解决方案2】:

    我没有用 HttpPostedFileBase 尝试过,但下面的方法有效,应该让您了解代码中发生的事情。

    public class Test
    {
        public static void UploadFileToFTP()
        {
    
            string ftpurl = "ftp://ServerName:21/test.txt";
    
            try
            {
                string filename = "F:\\testing.txt";
                string ftpfullpath = ftpurl;
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
                ftp.Credentials = new NetworkCredential();
    
                //ftp.KeepAlive = false;
                ftp.UsePassive = false;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;
                byte[] myFile = File.ReadAllBytes(filename);
                ftp.ContentLength = myFile.Length;
                Stream ftpstream = ftp.GetRequestStream();
                ftpstream.Write(myFile, 0, myFile.Length);
                ftpstream.Close();
                ftpstream.Flush();
            }
            catch (WebException e)
            {
                String status = ((FtpWebResponse)e.Response).StatusDescription;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多