gpt4 book ai didi

c# - .Net Core 应用程序无法将文件写入任何文件夹 OSX

转载 作者:行者123 更新时间:2023-11-30 15:56:06 27 4
gpt4 key购买 nike

我正在 OSX 中构建一个 .net core 2.0 应用程序,它是一个 WebAPI 应用程序,当一个 API 端点被命中时,它会创建一个带有虚拟数据的 .xlsx。当我尝试运行它 (dotnet run) 时,我得到了

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

我试过以 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;
}

这是类(class)的主要部分,我正在项目文件夹中创建文件(目前)。我看到有关为 CLI 授予我所有文件的读写权限的帖子,但这似乎并不理想。我也看到了 this关于在文件上设置属性,但这是 OpenXml 电子表格创建方法,据我所知,它没有任何关于设置文件权限的信息(无论如何它都需要在文件夹中)

任何帮助将不胜感激

最佳答案

我明白了,我发送的文件路径是文件夹路径而不是文件路径,问题已解决

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

关于c# - .Net Core 应用程序无法将文件写入任何文件夹 OSX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47554718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com