Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public void upload ()
- {
- String sourcefilepath ="C:\Users\TST\Desktop\TESTOWS.txt";
- String ftpurl = "1.1.1.1/LIBRARY";
- String ftpusername = "ACC";
- String ftppassword = "ACC";
- Stream requestStream = null;
- FileStream fileStream = null;
- FtpWebResponse uploadResponse = null;
- try
- {
- string filename = Path.GetFileName(sourcefilepath);
- string ftpfullpath = @"ftp://" + ftpurl + "/" + filename;
- FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(string.Format(ftpfullpath));
- uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
- //Since the FTP you are downloading to is secure, send
- //in user name and password to be able upload the file
- ICredentials credentials = new NetworkCredential(ftpusername, ftppassword);
- uploadRequest.Credentials = credentials;
- uploadRequest.UsePassive = true;
- uploadRequest.UseBinary = false;
- uploadRequest.KeepAlive = false;
- //UploadFile is not supported through an Http proxy
- //so we disable the proxy for this request.
- uploadRequest.Proxy = null;
- //uploadRequest.UsePassive = false; <--found from another forum and did not make a difference
- requestStream = uploadRequest.GetRequestStream();
- fileStream = File.Open(sourcefilepath, FileMode.Open);
- byte[] buffer = new byte[1024];
- int bytesRead;
- while (true)
- {
- bytesRead = fileStream.Read(buffer, 0, buffer.Length);
- if (bytesRead == 0)
- break;
- requestStream.Write(buffer, 0, bytesRead);
- }
- //The request stream must be closed before getting
- //the response.
- requestStream.Close();
- uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
- }
- catch (UriFormatException ex)
- {
- MessageBox.Show(ex.Message);
- }
- catch (IOException ex)
- {
- MessageBox.Show(ex.Message);
- }
- catch (WebException ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- if (uploadResponse != null)
- uploadResponse.Close();
- if (fileStream != null)
- fileStream.Close();
- if (requestStream != null)
- requestStream.Close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement