API POST 将文件保存到另一个系统驱动器的方法抛出 500 错误

API POST method to save a file to another system drive is throwing a 500 error

我有一个 .NetCore 3 API 控制器可以接收 POST 请求。

但是,我希望它将文件保存在不同的驱动器上。

F:\GameFiles\Media\UserScreens\Uploads

我正在我的方法中设置路径,如下所示。

但是每当我用 Postman 对其进行测试时,它只是说它遇到了一般的 500 错误。

[HttpPost]
public async Task<IActionResult> PostUserMedia([FromForm] IFormFile imageFile)
{

    string contentRootPath = _hostingEnvironment.ContentRootPath;

    var uploads = Path.Combine(contentRootPath, "F:\GameFiles\Media\UserScreens\Uploads");
    var filePath = Path.Combine(uploads, imageFile.FileName);

    using (var fileStream = new FileStream(filePath, FileMode.Create))
    {
        await imageFile.CopyToAsync(fileStream);
    }
    return Ok();
}

在 Postman 中,地址是:http://x.x.x.x/api/GameFilesAndMedia

我正在使用 'form-data' 正文并在 Postman 提示时选择测试文件。

我可能做错了什么?

谢谢!

您需要在路径中使用双反斜杠。

"F:\GameFiles\Media\UserScreens\Uploads" 而不是 "F:\GameFiles\Media\UserScreens\Uploads"

要使用应用外的文件夹,直接指定文件上传位置:

[HttpPost]
public async Task<IActionResult> PostUserMedia([FromForm] IFormFile imageFile)
{

    var filePath = Path.Combine(@"F:\GameFiles\Media\UserScreens\Uploads", imageFile.FileName);

    using (var fileStream = new FileStream(filePath, FileMode.Create))
    {
        await imageFile.CopyToAsync(fileStream);
    }
    return Ok();
}