Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.IO.IOException:尝试在web api中保存FormFile时,参数无效_C#_.net Core_.net Core 3.0 - Fatal编程技术网

C# System.IO.IOException:尝试在web api中保存FormFile时,参数无效

C# System.IO.IOException:尝试在web api中保存FormFile时,参数无效,c#,.net-core,.net-core-3.0,C#,.net Core,.net Core 3.0,更新: 看来问题出在Docker身上。使用IIS Express构建项目时,一切都按预期工作。 我将不得不调查为什么Docker会引起这个问题 我正在尝试将发送到API的映像保存到本地磁盘 但是我遇到了图像实际存储的问题。 试图保存时,我得到一个System.IO.IOException 以下是API方法: [HttpPost] public IActionResult Post([FromForm] IFormFile file) { var filePath = @"C:\" +

更新: 看来问题出在Docker身上。使用IIS Express构建项目时,一切都按预期工作。 我将不得不调查为什么Docker会引起这个问题

我正在尝试将发送到API的映像保存到本地磁盘

但是我遇到了图像实际存储的问题。 试图保存时,我得到一个
System.IO.IOException

以下是API方法:

[HttpPost]
public IActionResult Post([FromForm] IFormFile file)
{
     var filePath = @"C:\" + file.FileName + ".png";
     if(file.Length > 0)
     {
         using (var stream = new FileStream(filePath, FileMode.Create))
         {
             file.CopyTo(stream);
         }
     }
     return Ok();
 }
文件很好地进入,如果我在VisualStudio中调试,我可以看到它是正确的文件。 代码在
新文件流(filePath,FileMode.Create)
行中断

我尝试了不同的路径,如
C:\Pictures\
C:\users\myuser\Pictures\
,但似乎没有任何效果

例外情况详情如下:

An exception of type 'System.IO.IOException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Invalid argument'
Stack trace:
 >   at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
 >   at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
 >   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
 >   at System.IO.FileStream..ctor(String path, FileMode mode)

首先,尝试使用异步版本的
CopyTo
method->
wait file.copytosync(stream)
,这仅仅是出于性能原因

您可能已经在某个编辑器中打开了此文件,并收到错误:

进程无法访问文件“文件路径”,因为它正在运行 被另一个进程使用

如果是这样,只需关闭打开文件的编辑器,一切都应按预期进行

另一个技巧:尝试使用
path,而不是通过连接来获取路径。组合

评论后编辑: 正如@PanagiotisKanavos在评论中提到的:

c:\whatever.png对Linux无效。而Docker容器将 比IIS有更多的限制。如果你想保留那些文件 您必须将它们存储在映射的存储位置。还有别的吗? 将在容器关闭时丢失


您提到从docker容器中运行应用程序时出现错误。默认情况下,docker在Linux shell中运行容器,Linux不识别路径中的反斜杠,相反,您可以在路径中使用正斜杠(/),这在windows和Linux上都适用。建议使用组合路径的方法,而不是字符串串联。您还可以检查Path.altdirectorysepartorchar和Path.directorysepartorchar属性,这些属性也可以使用


还请注意,Linux上不存在c:\s,您应该指定应用程序的相对路径或使用Linux上的有效目录。

您得到的确切错误描述是什么?可能是权限问题?当您试图保存对文件的更改时,是否使用其他程序打开了文件?异常消息是什么?请编辑您的问题并添加有关错误的所有详细信息。我已用异常详细信息更新了帖子。@Gurkmeja101实际文件名是什么?你试过调试代码吗?错误抱怨FileStream构造函数的参数无效。无论如何,您都应该感谢ASP.NET Core不允许您写入
C:\
。你能想象一个格式错误的POST请求可能造成的损害吗?如果文件名以“Windows\System32”开头怎么办?