0

I want to delete a folder and its content before returning some data from the browser.

I am using ASP .NET MVC 5 and just javascript with some DevExpress components.

The method should return a HttpResponseMessage, which is the excel file. The file is created in a temp folder and after that I want to delete it.

I get always an "UnauthorizedAccessException" exception.

Any idea how the logic flow should be to return the file and delete it? I was thinking on calling a new method after finishing the ExportExcelChart but maybe there is a better way to achieve the expected result.

How

JS (browser)

function SaveChart(s, e) {
    //debugger;
    if (e.item.name === 'mnuSaveToDisk') {
        averageChart.printOptions.landscape = true;
        averageChart.printOptions.SetSizeMode('Stretch');
        averageChart.SaveToDisk('pdf', "average curves");
    } else if (e.item.name == 'mnExportChart') {
        ChartLoadingPanel.Show();
        @*$.get('@Url.Action("ExportExcelChart", "Mindboard")', {}, null);*@
        window.location = '@Url.Action("ExportExcelChart", "Mindboard")';
        ChartLoadingPanel.Hide();
    }
}

Controller

public HttpResponseMessage ExportExcelChart()
{
    try
    {
        // create excel file
        String tmpRandomFolderName = DataService.RandomString(20);
        string tmpFolderPath = Path.Combine(Server.MapPath("~/App_Data"), "TEMP", tmpRandomFolderName);
        string tmpOriginFile = Server.MapPath("~/App_Data") + "/ExportChartAverage.xlsx";
        string tmpNewFile = tmpFolderPath + "/ExportChartAverage.xlsx";

        if (!Directory.Exists(tmpFolderPath))
            Directory.CreateDirectory(tmpFolderPath);

        System.IO.File.Copy(tmpOriginFile, tmpNewFile);

        // open excel file
        FileInfo tmpExcelFile = new FileInfo(tmpNewFile);
        ExcelPackage pck = new ExcelPackage(tmpExcelFile);
        var ws = pck.Workbook.Worksheets[1];

        // do stuff...

        pck.SaveAs(Response.OutputStream);
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=ExportChartAverage.xlsx");

        if (Directory.Exists(tmpFolderPath))
        {
            if (System.IO.File.Exists(tmpNewFile))
                System.IO.File.Delete(tmpNewFile);
            Directory.Delete(tmpFolderPath, true);
        }

        var response = new HttpResponseMessage(HttpStatusCode.OK);
        return response;
    }
    catch (IOException ex)
    {
        var response = new HttpResponseMessage(HttpStatusCode.NotFound);
        response.Content = new StringContent(ex.Message);

        return response;
        //throw ex;
    }
    catch (UnauthorizedAccessException ex)
    {
        var response = new HttpResponseMessage(HttpStatusCode.NotFound);
        response.Content = new StringContent(ex.Message);

        return response;
        //throw ex;
    }
    catch (Exception)
    {

        throw;
    }
}
blfuentes
  • 2,731
  • 5
  • 44
  • 72
  • Read this question: http://stackoverflow.com/q/2041717/5311735 – Evk Nov 14 '16 at 09:43
  • in which line exception appears? Maybe IIS/pool user has no rights to delete directory from disc? – P.K. Nov 14 '16 at 09:50
  • @P.K. in the `System.IO.File.Delete(tmpNewFile);` @Evk I am trying to implement the Attribute solution. – blfuentes Nov 14 '16 at 09:56
  • ok, but in my opinion problem is on OS level. YOu have to find as which user your script is started and this user must the right to modify/delete directory. GL. – P.K. Nov 14 '16 at 10:06

0 Answers0