文件上传并保存在Blazor服务器端

时间:2019-10-03 07:30:48

标签: file blazor blazor-server-side

如何在Blazor中使用HttpPostedFileBase在数据库中上传和保存文件

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase postedFile)
    {
        byte[] bytes;
        using (BinaryReader br = new BinaryReader(postedFile.InputStream))
        {
            bytes = br.ReadBytes(postedFile.ContentLength);
        }
        FilesEntities entities = new FilesEntities();
        entities.tblFiles.Add(new tblFile
        {
            Name = Path.GetFileName(postedFile.FileName),
            ContentType = postedFile.ContentType,
            Data = bytes
        });
        entities.SaveChanges();
        return RedirectToAction("Index");
    }

0 个答案:

没有答案