我遇到通过HTTPS远程服务器连接FTP和上传文件的问题。 我有第三方服务器 Hostgator 与转销商帐户,在SSL证书之前,我上传的文件使用以下代码正在运行。我已购买SSL证书。现在,如果我使用相同的代码上传文件,它不起作用,给我异常 System.Net.WebException 和系统错误 .Kindly帮助将文件上传到远程服务器目录FTP over HTTPS。
public string UploadImageURL(string ImageFileName, string ImageBase64, string UploadDirectory)
{
string ftpurl = "ftp://www.domain.com/";
var username = "xxxxxx";
var password = "aaaaaa";
ImageFileName = "testing.jpeg";
string UploadDirectory = "httpdocs/ImageFolder/";
try
{
//string PureFileName = new FileInfo(VoiceModel.FileName).Name;
if (ImageFileName != null && ImageBase64 != null)
{
String uploadUrl = String.Format("{0}{1}/{2}", ftpurl, UploadDirectory, ImageFileName);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
req.Proxy = null;
req.Method = WebRequestMethods.Ftp.UploadFile;
req.Credentials = new NetworkCredential(username, password);
req.EnableSsl = true;
req.UseBinary = true;
req.UsePassive = true;
//Connect to SSL Port (990)
ServicePoint sp = req.ServicePoint;
byte[] data = Convert.FromBase64String(ImageBase64);
req.ContentLength = data.Length;
req.Timeout = -1;
Stream stream = req.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
FtpWebResponse res = (FtpWebResponse)req.GetResponse();
ImageUrl = Convert.ToString(res.ResponseUri);
ImageUrl = ImageUrl.Replace("ftp://", "http://");
ImageUrl = ImageUrl.Replace("httpdocs/", "");
ImageUrl = ImageUrl.Trim();
return ImageUrl;
}
else
{
return ImageUrl = "";
}
}
catch (Exception)
{
throw;
}
}
答案 0 :(得分:0)
您好,您可以在HTTPS中使用“以下功能”,效果很好
public string UploadFile(string Ext,string FtpUrl,string fileName,string userName,string password,string UploadDirectory,Stream inputstream,double NewWidth,double NewHeight)
{
Bitmap bmp = new Bitmap(inputstream);
Graphics canvas = Graphics.FromImage(bmp);
var newWidth = (int)(bmp.Width * NewWidth);
var newHeight = (int)(bmp.Height * NewHeight);
Bitmap bmpNew = new Bitmap(newWidth, newHeight);
canvas = Graphics.FromImage(bmpNew);
canvas.DrawImage(bmp, new Rectangle(0, 0,
bmpNew.Width, bmpNew.Height), 0, 0, bmp.Width, bmp.Height,
GraphicsUnit.Pixel);
bmp = bmpNew;
MemoryStream ms = new MemoryStream();
if (Ext == ".jpg" || Ext == ".jpeg")
{
bmp.Save(ms, ImageFormat.Jpeg);
}
else if (Ext == ".png")
{
bmp.Save(ms, ImageFormat.Png);
}
else if (Ext == ".gif")
{
bmp.Save(ms, ImageFormat.Gif);
}
else if (Ext == ".btm")
{
bmp.Save(ms, ImageFormat.Bmp);
}
else if (Ext == ".tiff")
{
bmp.Save(ms, ImageFormat.Tiff);
}
ms.Position = 0;
byte[] data = ms.ToArray();
byte[] fileBytes = ms.ToArray();
string PureFileName = new FileInfo(fileName).Name;
FileInfo oFile = new FileInfo(fileName);
String uploadUrl1 = String.Format("{0}{1}", FtpUrl, UploadDirectory);
bool result = MFtpDirectoryExists(uploadUrl1, userName, password);
String uploadUrl = String.Format("{0}{1}{2}", FtpUrl, UploadDirectory + "/", PureFileName);
u.MsgBox(uploadUrl);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
req.Method = WebRequestMethods.Ftp.UploadFile;
req.Credentials = new NetworkCredential(userName, password);
req.UseBinary = true;
req.ContentLength = data.Length;
Stream stream = req.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
FtpWebResponse res = (FtpWebResponse)req.GetResponse();
return PureFileName;
}