我使用稍微修改的C#腳本,我發現(可能在這裏)在SSIS做到這一點。如果有人認出這段代碼,我會很樂意撤回這個答案。
現在,我不是C#開發人員,所以如果此代碼是可怕的,我懇求無知。它確實從一個網站上下載了一個文件,但是,這正是我需要它做的。
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Net;
public void Main()
{
//Init Variables
string downloadurl = Dts.Variables["User::downloadlink"].Value.ToString();
string targetdir = Dts.Variables["User::targetdir"].Value.ToString();
string targetfile = Dts.Variables["User::targetfilename"].Value.ToString();
bool fileexists = File.Exists(targetdir + targetfile);
bool overwrite = Convert.ToBoolean(Dts.Variables["User::overwritedlfile"].Value);
string webpassword = "";
string weblogin = "";
try
{
// Logging start of download
bool fireAgain = true;
Dts.Events.FireInformation(0, "Download File", "Start downloading " + Dts.Variables["User::downloadlink"].Value.ToString(), string.Empty, 0, ref fireAgain);
// For authenticated downloads
if ((webpassword != "" || weblogin != "") && (fileexists == false || (overwrite == true && fileexists == true)))
{
weblogin = Dts.Variables["User::weblogin"].Value.ToString();
webpassword = Dts.Variables["User::webpassword"].Value.ToString();
// Create a webclient to download a file
using (WebClient mySSISWebClient = new WebClient())
{
//Insert credentials
mySSISWebClient.Credentials = new NetworkCredential(weblogin, webpassword);
//Download file (source, destination)
mySSISWebClient.DownloadFile(downloadurl, targetdir + targetfile);
}
//// Logging end of download
Dts.Events.FireInformation(0, "Download File", "Finished downloading " + Dts.Variables["User::downloadlink"].Value.ToString(), string.Empty, 0, ref fireAgain);
//Console.WriteLine("Download Complete");
// Quit Script Task succesful
Dts.TaskResult = (int)ScriptResults.Success;
}
// For regular downloads
if (fileexists == false || (overwrite == true && fileexists == true))
{
// Create a webclient to download a file
using (WebClient mySSISWebClient = new WebClient())
{
mySSISWebClient.DownloadFile(downloadurl, targetdir + targetfile);
}
//// Logging end of download
Dts.Events.FireInformation(0, "Download File", "Finished downloading " + Dts.Variables["User::downloadlink"].Value.ToString(), string.Empty, 0, ref fireAgain);
//Console.WriteLine("Download Complete");
// Quit Script Task succesful
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{
Dts.Events.FireError(0, "Download File", "Download failed: " + (targetfile + " already exists in the target directory"), string.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
throw new Exception(targetfile + " already exists in the target directory");
}
}
catch (Exception ex)
{
// Logging why download failed
Dts.Events.FireError(0, "Download File", "Download failed: " + ex.Message, string.Empty, 0);
//Console.WriteLine(ex.ToString());
// Quit Script Task unsuccesful
Dts.TaskResult = (int)ScriptResults.Failure;
}
通常,如果您實際發佈了一些代碼,則會得到更好的響應。 – 2014-12-03 17:07:26