我正在向网站添加一项功能,以便会员可以将自己的照片上传到他们的个人资料中。我遇到的问题是我的 FTP 功能在我运行它自己的 localhost 时工作正常。但是,在我将站点放在 GoDaddy 的服务器上并尝试从那里进行 FTP 后,它不起作用。我明白了
无法连接到远程服务器
这是我的代码:
protected string savePath = Path.GetTempPath();
protected string saveThumbPath = Path.GetTempPath() + "/Thumb";
Guid g;
protected void UploadButton_Click(object sender, EventArgs e)
{
bool worked = false;
if (FileUploadControl.HasFile)
{
try
{
g = Guid.NewGuid();
string filename = Path.GetFileName(FileUploadControl.FileName);
Bitmap src = Bitmap.FromStream(FileUploadControl.PostedFile.InputStream) as Bitmap;
Bitmap thumb = Bitmap.FromStream(FileUploadControl.PostedFile.InputStream) as Bitmap;
// Resize the bitmap data
//Create the large image
Bitmap result = ProportionallyResizeBitmap(src, 800, 600);
//string saveName = Server.MapPath(savePath) + g + filename;
string saveName = savePath + g + filename;
result.Save(saveName, ImageFormat.Jpeg);
//Create the thumbnail
result = ProportionallyResizeBitmap(thumb, 200, 150);
//string saveThumbName = Server.MapPath(saveThumbPath) + g + filename;
string saveThumbName = saveThumbPath + g + filename;
result.Save(saveThumbName, ImageFormat.Jpeg);
StatusLabel.Text = "Upload status: File uploaded!";
worked = true;
Thumbholder.Value = "Thumb" + g + filename;
Photoholder.Value = g + filename;
// Get the object used to communicate with the server.
//If the specified proxy is an HTTP proxy. only the DownloadFile, ListDirectory and ListDirectoryDetails commands are supported
//get the object used to communicate with the server
System.Net.FtpWebRequest request = System.Net.WebRequest.Create("ftp://mydomain/newcorvetteclub/Images/" + g + filename) as System.Net.FtpWebRequest;
//this example assumes the FTP site uses anoymous login on
//NetWorkCredentials provides credentials for password-based authentication such as digest, basic, NTLM
request.Credentials = new System.Net.NetworkCredential("username", "password");
//Copy the contents of the file to the request stream
byte[] fileContents = null;
if (FileUploadControl.HasFile)
{
//fileContents = FileUploadControl.FileBytes;
fileContents = File.ReadAllBytes(saveName);
}
else
{
Response.Write("you need to provide a file");
return;
}
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
request.ContentLength = fileContents.Length;
//GetReequestStream: retrieves the stream used to upload data to an FTP server.
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
System.Net.FtpWebResponse response = request.GetResponse() as System.Net.FtpWebResponse;
//Response.Write("Upload file complete, status: " + response.StatusDescription);
response.Close();
request = System.Net.WebRequest.Create("ftp://mydomain/newcorvetteclub/Images/Thumb" + g + filename) as System.Net.FtpWebRequest;
request.Credentials = new System.Net.NetworkCredential("username", "password");
if (FileUploadControl.HasFile)
{
fileContents = File.ReadAllBytes(saveThumbName);
}
else
{
Response.Write("you need to provide a file");
return;
}
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
request.ContentLength = fileContents.Length;
//GetReequestStream: retrieves the stream used to upload data to an FTP server.
requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
response = request.GetResponse() as System.Net.FtpWebResponse;
//Response.Write("Upload file complete, status: " + response.StatusDescription);
另一个奇怪的事情是我的电子邮件客户端正在做类似的事情。只要它通过 localhost 运行,它就可以正常工作,但是当它从 GoDaddy 的服务器运行时会超时。任何帮助将不胜感激。