Created On:  08 October 2012


Customer is using Visual COBOL for Visual Studio and needs to dynamically upload a file to an ftp site using the .NET Framework classes.  Can you provide an example of how this can be done?


Here is an example of uploading a file to an ftp site using Visual COBOL for Visual Studio managed code and the .NET Framework classes.

$set ilusing"System.Net"
$set ilusing"System.IO"
program-id. Program1 as "testftp.Program1".
data division.
working-storage section.
01 cFtpWebRequest type System.Net.FtpWebRequest.
01 cFtpWebResponse type System.Net.FtpWebResponse.
01 cFileInfo type FileInfo.
01 fileContents type Byte occurs any.
01 cFileStream type FileStream.
01 writer type Stream.
procedure division.

   set cftpWebRequest to type FtpWebRequest::Create(new type Uri("")) as type FtpWebRequest
   set cFtpWebRequest::Method to type "System.Net.WebRequestMethods Ftp"::"UploadFile"
   set cftpWebRequest::Proxy to null
   set cftpWebRequest::UseBinary to true
   set cftpWebRequest::Credentials to new NetworkCredential("username", "password")
   set cFileInfo to new FileInfo("c:\\temp\\testfile.txt"
   set size of fileContents to cFileInfo::Length
   set cFileStream to cFileInfo::OpenRead
   invoke cFileStream::Read(fileContents, 0, type Convert::ToInt32(cFileInfo::Length))
   set writer to cftpWebRequest::GetRequestStream
   invoke writer::Write(fileContents, 0, fileContents::Length)
   invoke writer::Close
   set cftpWebResponse to cFtpWebRequest::GetResponse as type FtpWebResponse
   display cftpWebResponse::StatusDescription
catch ex as type WebException
   display ex::Message