简体   繁体   中英

.Net Core app can't write file to any folder OSX

I am building a .net core 2.0 app in OSX, its a WebAPI app that when an API endpoint is hit creates a .xlsx with dummy data. When I try to run it (dotnet run) I get

System.UnauthorizedAccessException: Access to the path '/Users/myuser/projects/myproject' is denied. ---> System.IO.IOException: Permission denied

I have tried running it as sudo and changing the folder it is writing to and neither helped

// GET api/values/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
        CreatePackage("./");
        return "value";
    }

    public void CreatePackage(string filePath)
    {
        using (SpreadsheetDocument package = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
        {
            CreateParts(package);
        }
    }

    private void CreateParts(SpreadsheetDocument document)
    {
        WorkbookPart workbookPart = document.AddWorkbookPart();
        GenerateWorkbookPartContent(workbookPart);

        WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>("rId1");
        GenerateWorksheetPartContent(worksheetPart);
    }

    private void GenerateWorkbookPartContent(WorkbookPart workbookPart)
    {
        Workbook workbook = new Workbook();
        workbook.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");

        Sheets sheets = new Sheets();
        Sheet sheet = new Sheet() { Name = "Sheet", SheetId = (UInt32Value)1U, Id = "rId1" };
        sheets.Append(sheet);

        workbook.Append(sheets);
        workbookPart.Workbook = workbook;
    }

    private void GenerateWorksheetPartContent(WorksheetPart worksheetPart)
    {
        Worksheet worksheet = new Worksheet();
        SheetData sheetData = new SheetData();

        Row row = new Row();
        Cell cell = new Cell() { CellReference = "A1", DataType = CellValues.InlineString };
        InlineString inlineString = new InlineString();
        Text text = new Text();
        text.Text = "hello";
        inlineString.Append(text);
        cell.Append(inlineString);
        row.Append(cell);

        sheetData.Append(row);
        worksheet.Append(sheetData);
        worksheetPart.Worksheet = worksheet;
    }

This is the main parts of the class, I am creating the file (for now) in the project folder. I saw posts about giving read write permissions to all of my files for the CLI but that doesn't seem ideal. Also I saw this about setting attributes on files, but this is the OpenXml spreadsheet create method and as far as I can tell it doesnt have anything about setting file permissions (and it would need to be at the folder anyway)

Any help would be much appreciated

I figured it out, the filepath I was sending was a folderpath not a filepath, issue resolved

[HttpGet("{id}")]
public string Get(int id)
{
    CreatePackage("./testfile.xlsx");
    return "value";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM