Reputation: 922
I am adding a functionality to a website so that members can upload there own photos to their profile. The problem that I am having is my FTP capability works fine when I run it own localhost. However, after I put the site up on GoDaddy's server and try to FTP from there it does not work. I get
Unable to connect to remote server
Here is my code:
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);
Another weird thing is that my email client is doing something similar. It works fine as long as it is being run through localhost but then times out when it it being run from GoDaddy's servers. Any help would be greatly appreciated.
Upvotes: 2
Views: 1844
Reputation: 26
I can tell you why the email isn't going through you have to use "relay-hosting.secureserver.net" as your SMTP Server. As far as the FTP is concerned you can't do it on godaddy they block outgoing ftp. I just found that out.
Upvotes: 1
Reputation: 27923
The problem is in here, where you use the same stream for both of these calls:
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;
Streams have a current position, and so the first .FromStream
probably reads the whole stream, setting the position of the stream to the end of stream. To undo this, you need to reset it by setting stream.Position = 0
.
Upvotes: 0