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;
    }
}
4
  • Read this question: stackoverflow.com/q/2041717/5311735 Commented Nov 14, 2016 at 9:43
  • in which line exception appears? Maybe IIS/pool user has no rights to delete directory from disc? Commented Nov 14, 2016 at 9:50
  • @P.K. in the System.IO.File.Delete(tmpNewFile); @Evk I am trying to implement the Attribute solution. Commented Nov 14, 2016 at 9: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. Commented Nov 14, 2016 at 10:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.