2013-03-15 28 views
1

我在一月份就此发表了一篇文章,但它从未解决。我一直在制作一个ftp到我的Youtube团队网站的程序。出于某种奇怪的原因,生成的HTML代码可以正常传输,并且成功传输,但图像通过损坏。我确定二进制模式设置为true,权限全部正确。似乎没有任何工作。在FTP中破坏的图像,二进制模式

有人可以帮我吗?

这是我们的代码的一部分是有关我的问题:

namespace TMNGP_FTP 
{ 
    partial class Form1 
    { 
     // some functions and properties for the form 
     UriBuilder ftpurl; 
     String ftpurlstr = "ftp://tmngp.heliohost.org"; 
     static String ftpusername = "*******"; 
     static String ftppassword = "*******"; 

     public static string GenerateFileName(string context) 
     { 
      return context + DateTime.Now.ToString("yyyyMMddHHmmss") + ".html"; 
     } 

     public void openpic_Click(object sender, System.EventArgs e) 
     { 
      //Wrap the creation of the OpenFileDialog instance in a using statement, 
      //Rather than manually calling the dispose method to ensure proper disposal 
      using (OpenFileDialog dlg = new OpenFileDialog()) 
      { 
       dlg.Title = "Open Image"; 
       dlg.Filter = "png files (*.png)|*.png"; 

       if (dlg.ShowDialog() == DialogResult.OK) 
       { 

        string folderName = @"c:\TMNGP_Web_Files"; 

        string pathString = folderName + @"\htTemp"; 
        pathString = pathString + @"\imgs"; 

        if (!System.IO.Directory.Exists(pathString)) 
        { 
         System.IO.Directory.CreateDirectory(pathString); 
        } 

        string destFileName = pathString + @"\" + dlg.SafeFileName.ToString(); 

        System.IO.File.Copy(dlg.FileName, destFileName, true); 
        DisplImg.Image = new Bitmap(dlg.OpenFile()); 
        DisplImg.ImageLocation = destFileName; 
       } 
      } 

     } 

     private FtpClient ftpnew = null; 

     public void textgen_Click(object sender, System.EventArgs e) 
     { 

      string folderName = @"c:\TMNGP_Web_Files"; 

      string pathString = folderName + @"\htTemp"; 

      if (!System.IO.Directory.Exists(pathString)) 
      { 
       System.IO.Directory.CreateDirectory(pathString); 
      } 

      string fileName = GenerateFileName("HT"); 

      pathString = pathString + @"\" + fileName; 

      Console.WriteLine("Path to my file: {0}\n", pathString); 

      if (!System.IO.File.Exists(pathString)) 
      { 
       //System.IO.FileStream fs = System.IO.File.Create(pathString); 
       using (System.IO.StreamWriter file = new System.IO.StreamWriter(pathString)) 
       { 
        file.WriteLine("<div class='simple_overlay' id='news_archive/" + DisplImg.ImageLocation.Substring(31) + "' style='display:none;'>"); 
        file.WriteLine("<a class='close'></a>"); 
        file.WriteLine("<img src='news_archive/" + DisplImg.ImageLocation.Substring(31) + "'/>"); 
        file.WriteLine("<div class='details'>"); 
        file.WriteLine("<h3> " + txtTitle.Text + " </h3>"); 
        file.WriteLine("<h4> " + TxtInfo.Text + " </h4>"); 
        file.WriteLine("<p>" + Desctext.Text + "</p>"); 
        file.WriteLine("</div>"); 
        file.WriteLine("</div>"); 
       } 

       if(radioButton1.Checked) 
       { 
        ftpurl = new UriBuilder("ftp", "tmngp.heliohost.org", 21, "NGP/news_archive/"); 
        ftpurlstr = "/public_html/NGP/news_archive"; 
       } 
       else 
       { 
        ftpurl = new UriBuilder("ftp", "tmngp.heliohost.org", 21, "TM/news_archive/"); 
        ftpurlstr = "/public_html/TM/news_archive"; 
       } 

       try 
       { 
        //string filenametwo = System.IO.Path.GetFullPath(@"c:\TMNGP_Web_Files\htTemp\"+fileName); 
        string filenamethree = System.IO.Path.GetFullPath(DisplImg.ImageLocation.ToString()); 
        Console.WriteLine("{0}", filenamethree); 
        Console.WriteLine(pathString); 
        //string ftpfullpath = ftpurl; 
        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create((@"ftp://tmngp.heliohost.org" + ftpurlstr + fileName).ToString()); 
        Console.WriteLine("{0}", ftpurl + fileName); 
        ftp.Credentials = new NetworkCredential(ftpusername, ftppassword); 
        ftp.KeepAlive = true; 
        ftp.UseBinary = true; 
        ftp.Method = WebRequestMethods.Ftp.UploadFile; 
        StreamReader sourceStreamone = new StreamReader(@"c:\TMNGP_Web_Files\htTemp\" + fileName); 
        byte[] fileContentsone = Encoding.UTF8.GetBytes(sourceStreamone.ReadToEnd()); 
        sourceStreamone.Close(); 
        ftp.ContentLength = fileContentsone.Length; 
        Stream requestStreamone = ftp.GetRequestStream(); 
        requestStreamone.Write(fileContentsone, 0, fileContentsone.Length); 
        requestStreamone.Close(); 
        FtpWebResponse ftpresponseone = (FtpWebResponse)ftp.GetResponse(); 
        Console.WriteLine("Upload File Complete, status {0}", ftpresponseone.StatusDescription); 
        ftpresponseone.Close(); 

       } 
       catch (WebException ex) 
       { 
        throw ex; 
       } 
       try 
       { 
        string imgfile = DisplImg.ImageLocation.Substring(31); 
        FtpWebRequest ftp2 = (FtpWebRequest)FtpWebRequest.Create((@"ftp://tmngp.heliohost.org" + ftpurlstr + imgfile).ToString()); 
        ftp2.Credentials = new NetworkCredential(ftpusername, ftppassword); 
        ftp2.KeepAlive = true; 
        ftp2.UseBinary = true; 
        ftp2.Method = WebRequestMethods.Ftp.UploadFile; 
        StreamReader sourceStreamtwo = new StreamReader(DisplImg.ImageLocation.ToString()); 
        byte[] fileContentstwo = Encoding.UTF8.GetBytes(sourceStreamtwo.ReadToEnd()); 
        sourceStreamtwo.Close(); 
        ftp2.ContentLength = fileContentstwo.Length; 
        Stream requestStreamtwo = ftp2.GetRequestStream(); 
        requestStreamtwo.Write(fileContentstwo, 0, fileContentstwo.Length); 
        requestStreamtwo.Close(); 
        FtpWebResponse ftpresponsetwo = (FtpWebResponse)ftp2.GetResponse(); 
        Console.WriteLine("Upload File Complete, status {0}", ftpresponsetwo.StatusDescription); 
        ftpresponsetwo.Close(); 
       } 
       catch (Exception ex) 
       { 
        throw ex; 
       } 

       MessageBox.Show("FTP Complete"); 
      } 
      else 
      { 
       Console.WriteLine("File \"{0}\" already exists.", fileName); 
       return; 
      } 
     } 



     // a bunch of Windows Form Designer generated code ... 

    } 

} 
+0

一些不同的命名惯例您可以站在小于'在try..catch(前){抛出前;}'的反模式.. – Blindy 2013-03-15 04:03:16

回答

1

好这个代码是难以阅读,但是,它看起来像你正在使用的Encoding.UTF8.GetBytes() image(sourceStreamTwo)当你声明Byte [] fileContentTwo。

编辑 - 忘了提,你不需要StreamReader的 - 而不是使用一个FileStream:

FileStream sourceStreamtwo = new FileStream(DisplImg.ImageLocation.ToString(), FileMode.Open); 

改变,要

Byte[] fileContentTwo; 

using (BinaryReader br = new BinaryReader(sourceStreamtwo)) 
{ 
    fileContentsTwo = br.ReadBytes((int)sourceStreamtwo.Length); 
    // rest of code that deals with sourceStreamTwo 
} 

注意这是假定你是不是读从可能没有可用整个流的网络,请参阅Creating a byte array from a stream

在.net 4或更高版本中您可以使用Stream.CopyTo(),因为它可以处理流中的中断,因此可以更安全 - 请参阅上面的问题和答案以获取更多信息。

你应该很好。编码是用于文本,图像是二进制的。

也请考虑为您的变量:)

+0

好吧,我m试图做到这一点,但我得到一个错误,“最好的匹配”system.io.binaryreader.binaryreader(system.io.stream“有一些无效的争论'使用。以及作为streamreader不包含一个长度的定义 – sysmic 2013-03-15 04:34:50

+0

@ user1942062我正在写我的iPad,并没有检查所有的代码,只是现在检查它,并再次添加一些细节 – 2013-03-15 04:43:34

+0

谢谢你的帮助!我能够让它工作,因为你!我只需要再添加两个东西(只是因为我错误地编写了目录来放置文件) ell) – sysmic 2013-03-15 05:19:35