将文件从SSIS脚本包中调用的SSRS报告保存为PDF时出错



我正在尝试使用SSIS脚本任务中调用的HTTP连接下载PDF格式的SSRS报告。我收到错误"无法打开,因为它不是受支持的文件类型或文件已损坏。"。最终目标是将SSRS报告保存在存储在变量"AEIOutputStagingFileFullPath"中的文件夹位置中,作为PDF文件。以下是SSIS脚本任务中的实际代码

protected void SaveFile(string url, string localpath)
{
System.Net.HttpWebRequest loRequest;
System.Net.HttpWebResponse loResponse;
System.IO.Stream loResponseStream;
System.IO.FileStream loFileStream = new System.IO.FileStream(localpath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
byte[] laBytes = new byte[257];
int liCount = 1;
try
{
loRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
loRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
loRequest.Timeout = 600000;
loRequest.Method = "GET";
loResponse = (System.Net.HttpWebResponse)loRequest.GetResponse();
loResponseStream = loResponse.GetResponseStream();
while (liCount > 0)
{
liCount = loResponseStream.Read(laBytes, 0, 256);
loFileStream.Write(laBytes, 0, liCount);
}
loFileStream.Flush();
loFileStream.Close();
}
catch (Exception ex)
{
}
}
public void Main()
{
ConnectionManager conn = Dts.Connections["AccountSTP_HTTPCon"];
HttpClientConnection httpConn = new HttpClientConnection(conn.AcquireConnection(null));
string outputSTPpdf = Dts.Variables["User::AEIOutputStagingFileFullPath"].Value.ToString();
string pdfUrl = httpConn.ServerURL = @"http://vwdsrsdba0001/reports/report/TGAP/AccountChanges_STP&rs:Command=Render&rs:Format=PDF&rc:Toolbar=False";
SaveFile(pdfUrl, outputSTPpdf);

Dts.TaskResult = (int)ScriptResults.Success;
}

我使用webClient来实现这一点:

using (WebClient wc = new WebClient())
{
bool retry = true;
int retryCt = 0;
wc.Credentials = new System.Net.NetworkCredential([insert user],[insert password]);
while (retry)
{
try
{
wc.DownloadFile(@"http://[insert server]/Reportserver?/[insert folder]/[insert report name]&rs:Command=Render&rs:Format=PDF&rc:Toolbar=False&[query string of params are optional]"
, filepathName);
retry = false;
}
catch
{
retryCt++;
if (retryCt >= 10) { retry = false; }
}
}
}

最新更新