Hi There.
I recently implemented a FTP upload feature within an ASP.NET
application. I use the standard steps that are posted throughout the
web.
The basic funcationality works as a file is created on the FTP server
that is the identical size to what I am uploaded. However, if the
file is a non-text format (JPG, XLS) the resulting file that is posted
to FTP is not a valid file type. For example, I uploaded a JPG and
when I redownload that same JPG my photo software cannot recognize the
format.
Here is the code that I am using, as mentioned I feel it's consistent
with what is posted elsewhere.
Thanks in advance,
Jason
private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = _host + "/" + fileInf.Name;
FtpWebRequest reqFTP;
UriBuilder URI = new UriBuilder(uri);
URI.Scheme = "ftp";
reqFTP = (FtpWebRequest)FtpWebRequest.Create(URI.Uri);
reqFTP.Credentials = new NetworkCredential(_user, _password);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
reqFTP.UsePassive = false;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch(Exception ex)
{
throw;
}
}