Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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# 物理文件提供程序返回404_C#_File_Asp.net Core_Asp.net Core 2.0 - Fatal编程技术网

C# 物理文件提供程序返回404

C# 物理文件提供程序返回404,c#,file,asp.net-core,asp.net-core-2.0,C#,File,Asp.net Core,Asp.net Core 2.0,我正在尝试使用PhysicalFileProvider安全地访问文件。我已提供根路径作为本地临时文件的路径 public static string BASE_PATH = Path.GetTempPath(); public static string lastSavedFilePath = @"C:\Users\MyUser\AppData\Local\Temp\MyPicture.png" [HttpGet("{id}")] public async Task<FileResult

我正在尝试使用
PhysicalFileProvider
安全地访问文件。我已提供根路径作为本地临时文件的路径

public static string BASE_PATH = Path.GetTempPath();
public static string lastSavedFilePath = @"C:\Users\MyUser\AppData\Local\Temp\MyPicture.png"

[HttpGet("{id}")]
public async Task<FileResult> GetFileById(int id)
{
    //provides access to the physical file system, scoping all paths to a directory and its children
    IFileProvider provider = new PhysicalFileProvider(BASE_PATH);
    var fileInfo = provider.GetFileInfo(lastSavedFilePath);
    var fileStream = fileInfo.CreateReadStream();
    this._contentTypeProvider.TryGetContentType(lastSavedFilePath, out var mimeType);
    return File(fileStream, mimeType, "ProfilePicture.png");
}
public静态字符串BASE_PATH=PATH.GetTempPath();
公共静态字符串lastSavedFilePath=@“C:\Users\MyUser\AppData\Local\Temp\MyPicture.png”
[HttpGet(“{id}”)]
公共异步任务GetFileById(int id)
{
//提供对物理文件系统的访问,确定目录及其子目录的所有路径的范围
IFileProvider provider=新的物理文件提供程序(基本路径);
var fileInfo=provider.GetFileInfo(lastSavedFilePath);
var fileStream=fileInfo.CreateReadStream();
此.u contentTypeProvider.TryGetContentType(lastSavedFilePath,out var mimeType);
返回文件(fileStream,mimeType,“ProfilePicture.png”);
}

我正在使用一个文件进行测试,该文件肯定存在于我的本地路径中,但我似乎仍然得到一个
FileNotFound
错误?为什么对于作为BaseDirectory直接子级的文件,我会收到FileNotFound错误?

IFileProvider.GetFileInfo()
需要文件提供程序根目录中的文件子路径。根据:

子路径-标识文件的相对路径

在您的代码中,您将传递值为绝对路径的
lastSavedFilePath
“C:\Users\MyUser\AppData\Local\Temp\MyPicture.png”

要解决此问题,您应该传递文件的相对路径,例如:

var fileInfo = provider.GetFileInfo("MyPicture.png");

你是如何调用该方法的?@PatrickHofman通过postman在试图根据返回的
IFileInfo
文件创建读取流时抛出,你应该检查文件是否存在,并且你确定web用户有权访问该文件?@PatrickHofman似乎有一点“MyUser\AppData…”难道不应该只是获取系统临时文件夹并使用它吗?谢谢,当我回到我的桌面时,我会检查这个。是的,这就是问题所在,我真希望他们能自动为我检查。