I am trying to read the file name in an XML response from an API and use that to donwload the file in the browser when the button is clicked. The file is located on a different server and it gives me an error "InnerException = {"Access to the path 'C:\\Program Files (x86)\\IIS Express\\NADLvl3_20171218152333.csv' is denied."}".
protected void btnToNADLvl3CSV_Click(object sender, EventArgs e)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"http://brtinternal/PPGetReportAPI/NADLvl3.aspx");
var filename = "";
var node = xdoc.DocumentElement.FirstChild;
filename = node.InnerText;
if(filename != "FAIL")
{
try
{
Response.Clear();
Response.ContentType = "application/csv";
Response.AddHeader("Content-Disposition", "attachment;filename=NADLvl3.csv");
WebClient url = new WebClient();
url.DownloadFile(@"https://brtinternal/PPGetReportAPI/", filename);
Response.Flush();
Response.End();
}
catch(Exception Ex)
{
new ApplicationException(Ex.Message);
}
}
}
Please help me out.