Hi all,
I have a trivial problem but I couldn't solve it :(
I'm working in a web site (.net C#) and there's
a link which supposed to download a file from
ftp account directly.
The account information( for example) :
URL: ftp2.Sitename.com
Username: user
Password: pass
First I made direct links to the files in the code
but it gave me
(page cann't be displayed)
and if I typed the full link directly in the explorer
It doesn't even ask me for the login information !
Does anyone know how can I provide the account information
and the file link through code ?
Thnx in advance
Habeba KhaledPosted Sep 10, 2008, 2:48 AM
Thnx alot Sunny Chen
I'm working in that now
Sunny ChenPosted Sep 9, 2008, 8:34 PM
i don't know if the following code can help you
public void Download(string uri, string fileName, FtpDataMode mode)
{
try
{
FileStream fileStream = new FileStream(fileName, FileMode.Create);
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
ftpRequest.Credentials = new NetworkCredential("username", "password");
ftpRequest.KeepAlive = false;
ftpRequest.UseBinary = mode == FtpDataMode.Binary;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
Stream ftpStream = response.GetResponseStream();
long contentLength = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
fileStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
fileStream.Close();
response.Close();
}
catch (FtpException)
{
throw;
}
catch (Exception ex)
{
throw new FtpException(FtpException.EXCEPTION_TEXT, ex);
}
}