private FtpWebRequest ftpRequest = null; private Stream ftpStream = null; private int bufferSize = 2048;
protected void btnSubmit_Click(object sender, EventArgs e) { byte[] byteBuffer = new byte[bufferSize]; byte[] Data = fileArray.FileBytes; TransferFile(fileArray.PostedFile.FileName, Data)); }
public string TransferFile(string filename, byte[] fileContent) { string strMsg = string.Empty; try { /* Create an FTP Request */ String uploadUrl = String.Format("{0}/{1}/{2}", "ftp://address", "foldername", filename); ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uploadUrl)); /* Log in to the FTP Server with the User Name and Password Provided */ ftpRequest.Credentials = new NetworkCredential("Username", "Password"); /* When in doubt, use these options */ ftpRequest.UseBinary = true; ftpRequest.UsePassive = true; ftpRequest.KeepAlive = true; /* Specify the Type of FTP Request */ ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; ftpStream = ftpRequest.GetRequestStream(); /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */ try { ftpRequest.ContentLength = fileContent.Length; Stream requestStream = ftpRequest.GetRequestStream(); requestStream.Write(fileContent, 0, fileContent.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse(); strMsg = "File Upload Status: " + response.ToString(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } /* Resource Cleanup */ ftpStream.Close(); ftpRequest = null; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return strMsg; }
uploadUrl
Quote:Just want to strengthen ScottE's diagnosis, and be more specific. Timeout is most likely the issue. Either .Net implementation of FtpWebRequest is erroneous or the MSDN document has a typo, the default value of FtpWebRequest.Timeout is not -1 (Infinite). It is 100000 (100 seconds). In addition there is another timeout issue. A few tests have shown that responseStream always has a timeout value of 300000 (300 seconds). I do not how this value is assigned. Anyways, this value needs to be modified to accommodate large files. In summary, the solution is to set FtpWebRequest.Timeout and Stream.Timeout to a sufficiently large value
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)