1

我正在向网站添加一项功能,以便会员可以将自己的照片上传到他们的个人资料中。我遇到的问题是我的 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 的服务器运行时会超时。任何帮助将不胜感激。

4

2 回答 2

1

我可以告诉你为什么电子邮件没有通过你必须使用“relay-hosting.secureserver.net”作为你的 SMTP 服务器。就 FTP 而言,您无法在 Godaddy 上执行此操作,它们会阻止传出的 ftp。我刚刚发现了。

于 2012-06-14T22:33:56.357 回答
0

问题出在这里,您对这两个调用使用相同的流:

Bitmap src = Bitmap.FromStream(FileUploadControl.PostedFile.InputStream) as Bitmap;
FileUploadControl.PostedFile.InputStream.Position = 0; // try this
Bitmap thumb = Bitmap.FromStream(FileUploadControl.PostedFile.InputStream) as Bitmap;

流具有当前位置,因此第一个.FromStream可能读取整个流,将流的位置设置为流的末尾。要撤消此操作,您需要通过设置来重置它stream.Position = 0

于 2012-05-26T19:01:18.707 回答