我在一月份发了一篇关于这个的帖子,但从来没有解决过。我一直在开发一个 ftp 到我的 Youtube Teams 网站的程序。出于某种奇怪的原因,生成的 HTML 代码可以正常工作并成功传输,但图像已损坏。我确保二进制模式设置为 true 并且权限都正确。似乎没有任何效果。
有人可以帮助我吗?
这是与我的问题相关的守则部分:
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 ...
}
}