postman 无法访问已关闭的文件

wbgh16ku  于 2022-12-23  发布在  Postman
关注(0)|答案(1)|浏览(125)

在 Postman 返回无法访问关闭的文件。我想返回的个人资料图片路径保存到数据库,同时添加图片保存在wwwroot文件夹上的服务器。它总是返回无法访问关闭的文件。我不知道为什么。如果需要,我会把添加个人资料图片的代码。

public async Task<IActionResult> GetProfilePicture([FromQuery] string userId)
                {
                    FileStreamResult file;
                    if (userId == null)
                        return BadRequest("Something went wrong");
    enter code here
             // searching for user to get picture path that are saved into database
                    var userPP = await _context.Users.FindAsync(userId);
                    if (userPP == null)
                        return BadRequest("Something went wrong! Try again");
        // getting extension from file path
                    var ext = Path.GetExtension(userPP.ProfilePicture);
        
                    string contentType;
    // sending file with right contentType depending on extension
                    switch (ext)
                    {
                        case ".jpg":
                        case ".jpeg":
                            contentType = "image/jpeg";
                            break;
                        case ".png":
                            contentType = "image/png";
                            break;
                        case ".gif":
                            contentType = "image/gif";
                            break;
                        default:
                            contentType = "application/octet-stream";
                            break;
                    }
        //opening file stream to get image
                    using (var fs = new FileStream(userPP.ProfilePicture, FileMode.Open, FileAccess.Read))
                    {
        
                        file = File(fs, contentType);
                        return Ok(file);
                    }
                }
t1qtbnec

t1qtbnec1#

刚删除using语句,因为它在实际发送到前端之前关闭文件

相关问题