Tuesday, 8 October 2019

Downloading a file from a web server using SSIS script task

For downloading the files from the server we can use FTP task but for the some cases we need to write the C# code to download the files from the web server. We need to use the script task to achieve this.
For the example I am using below web URL to download the csv file.
Now I am creating the HTTP connection.
  

Click on the Test connection
  

Click OK. A new HTTP connection manager is created I named it as Web_server.
Now I am creating a flat file connection where we are going to store the downloaded file.
 

Named file path.
Now taking script task
 

In C# VSTA we need to write the below code.
  // Get your newly added HTTP Connection Manager
            Object HTTPConnectionManager = Dts.Connections["Web_Server"].AcquireConnection(null);
            // Create a new connection
            HttpClientConnection WebProductConnection = new HttpClientConnection(HTTPConnectionManager);
            // Get the location from the Flat File Connection Manager
            string DownloadLocation = Dts.Connections["File_path"].AcquireConnection(Dts.Transaction).ToString();
            try
            {
                // Logging start of download (optional)
                bool fireAgain = true;
                Dts.Events.FireInformation(0, "Download", "Downloading " + WebProductConnection.ServerURL, string.Empty, 0, ref fireAgain);
                // Download the file and replace the current CSV
                WebProductConnection.DownloadFile(DownloadLocation, true);
                // Logging end of download (optional)
                Dts.Events.FireInformation(0, "Download", "Saved " + DownloadLocation, string.Empty, 0, ref fireAgain);
                // Quit Script Task succesful
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            catch (Exception ex)
            {
                // Logging why download failed
                Dts.Events.FireError(0, "Download", "Failed: " + ex.Message, string.Empty, 0);

                // Quit Script Task unsuccesful
                Dts.TaskResult = (int)ScriptResults.Failure;
            }

Now package is ready to run
Before running this package record in the file.
  

Now I am running this package.
 

Packaged executed successfully. See the log.
  

Now see the file 

Reference: http://microsoft-ssis.blogspot.com/2011/05/download-source-file-from-website-with.html

1 comment:

  1. If i have file's that i am getting on a weekly basis with datetimestamp, how can i download?

    ReplyDelete

If you have any doubt, please let me know.

Popular Posts