[英].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. 我正在OSX中构建一个.net核心2.0应用程序,它是一个WebAPI应用程序,当命中API端点时会创建带有虚拟数据的.xlsx。 When I try to run it (dotnet run) I get 当我尝试运行它(dotnet运行)时,我得到了
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 我已经尝试将其作为sudo运行并更改它正在写入的文件夹,但都没有帮助
// 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. 我看到有关为CLI的所有文件提供读写权限的帖子,但这似乎并不理想。 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) 我也看到了关于在文件上设置属性的问题,但这是OpenXml电子表格创建方法,据我所知,它没有任何关于设置文件权限的东西(无论如何它都需要在文件夹中)
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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.