Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.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# 在url保存到数据库后显示图像_C#_Asp.net_Model View Controller_Core - Fatal编程技术网

C# 在url保存到数据库后显示图像

C# 在url保存到数据库后显示图像,c#,asp.net,model-view-controller,core,C#,Asp.net,Model View Controller,Core,我正在尝试将图像和视频保存到单独的文件夹中,而不是在SQL中,因为从我所读到的内容来看,最好不要将其存储在那里。我可以拍摄照片,然后将URL字符串保存到数据库中。文件正在保存到正确的位置 当我打开视图显示图像后,它就不显示了。我可以看到这是一个断开的链接。但不知道如何纠正它 控制员: [HttpPost] public IActionResult AddPost(AddPostViewModel addPost, IFormFile file) { //Sett

我正在尝试将图像和视频保存到单独的文件夹中,而不是在SQL中,因为从我所读到的内容来看,最好不要将其存储在那里。我可以拍摄照片,然后将URL字符串保存到数据库中。文件正在保存到正确的位置

当我打开视图显示图像后,它就不显示了。我可以看到这是一个断开的链接。但不知道如何纠正它

控制员:

[HttpPost]
    public  IActionResult AddPost(AddPostViewModel addPost, IFormFile file)
    {
        //Setting the extra data need for the post
        addPost.post.DateModified = DateTime.Now;
        addPost.post.DatePosted = DateTime.Now;

        //Saving the image to a location 
        SaveImage saveImage = new SaveImage();
        Task<String> filePath = saveImage.UploadFile(file);

        addPost.post.PostImageVideo = filePath.Result;



        //Adding and saving the post to the database and then returning to the admin panel. 
        _context.Post.Add(addPost.post);
        _context.SaveChanges();

        ViewBag.SessionUserFirstName = HttpContext.Session.GetString("UserFirstName");
        ViewBag.isAdmin = HttpContext.Session.GetString("IsAdmin");
        return RedirectToAction("AdminPanel");
    }
[HttpPost]
公共IActionResult AddPost(AddPostViewModel AddPost,IFormFile文件)
{
//设置发布所需的额外数据
addPost.post.DateModified=DateTime.Now;
addPost.post.datepost=DateTime.Now;
//将图像保存到某个位置
SaveImage SaveImage=新建SaveImage();
任务文件路径=saveImage.UploadFile(文件);
addPost.post.PostImageVideo=filePath.Result;
//将帖子添加并保存到数据库,然后返回管理面板。
_context.Post.Add(addPost.Post);
_SaveChanges();
ViewBag.SessionUserFirstName=HttpContext.Session.GetString(“UserFirstName”);
ViewBag.isAdmin=HttpContext.Session.GetString(“isAdmin”);
返回重定向到操作(“AdminPanel”);
}
正在调用用于保存图像的方法:

public async Task<String> UploadFile(IFormFile file)
    {
        var fileName = "";
        var filePath = "";
        //adding the filepath to the database and saving the path. 
        if (file != null && file.Length > 0)
        {
             fileName = Path.GetFileName(file.FileName);
             filePath = Path.Combine(Directory.GetCurrentDirectory(),@"~/wwwroot/images/", fileName);

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


        return filePath.ToString();

    }
public异步任务上传文件(ifformfile)
{
var fileName=“”;
var filePath=“”;
//将文件路径添加到数据库并保存路径。
if(file!=null&&file.Length>0)
{
fileName=Path.GetFileName(file.fileName);
filePath=Path.Combine(Directory.GetCurrentDirectory(),@“~/wwwroot/images/”,文件名);
使用(var fileStream=newfilestream(filePath,FileMode.Create))
{
等待file.CopyToAsync(fileStream);
}
}
返回filePath.ToString();
}
然后是从实体中拉入链接或从数据库中拉入链接的视图

@{
ViewData[“Title”]=“Post”;
}
@模型、职位、头衔
@模型体

我想我有:

调用saveImage方法时,我返回的是文件路径。我让它返回文件名,并在前面加上额外的字符串保存到数据库中,如下所示:

 addPost.post.PostImageVideo = "/images/" + fileName.Result;

这是否是正确的方法,还不确定,但它是有效的

您可以使用
FileContentResult
将图像作为要查看的文件返回

public class ImageController : Controller
{
    private readonly IHostingEnvironment _appEnvironment;

    public ImageController(IHostingEnvironment appEnvironment)
    {
        _appEnvironment = appEnvironment;
    }

    public IActionResult Index()
    {
        return View();
    }

    public FileContentResult ShowImage()
    {
        string fileName = Path.Combine(_appEnvironment.WebRootPath, "images\\avatar.png").Replace("\\", "/");

        byte[] imageData = null;
        FileInfo fileInfo = new FileInfo(fileName);
        long imageFileLength = fileInfo.Length;
        FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        imageData = br.ReadBytes((int)imageFileLength);

        return File(imageData, "image/png");
    }
}
视图

<img src="@Url.Action("ShowImage", "Image", new { area = "" })" class="user-image" alt="User Image">


数据库中存储的图像uri值是多少?在HTML中为图像的uri呈现了什么值?我正在尝试将图片存储在wwwroot/images文件夹中。看起来这些照片都在那里。将combine语句调整为:lePath=Path.combine(Directory.GetCurrentDirectory(),“wwwroot/images/”,fileName)之后,数据库中现在是什么;C:\Users\justi\source\repos\Blog\Blog\wwwroot/images/2years.jpg