gpt4 book ai didi

httppostedfilebase - 在 SQL 中将 httppostedfilebase 另存为图像

转载 作者:行者123 更新时间:2023-12-02 22:29:52 25 4
gpt4 key购买 nike

在我的 MVC 代码中,我得到了一个 httppostedfilebase 类型的图像。我的 SQL 数据库中有相应的图像类型列。

我需要知道如何在我的数据库中将此 httppostedfilebase 类型转换/保存为图像。

最佳答案

创建一个将 HttpPostedFileBase 对象转换为文件的函数

public byte[] ConvertToByte(HttpPostedFileBase file)
{
byte[] imageByte = null;
BinaryReader rdr = new BinaryReader(file.InputStream);
imageByte = rdr.ReadBytes((int)file.ContentLength);
return imageByte;
}

在你的 Controller 中编写这样的代码

public ActionResult Create(AdminDetailsViewModel viewmodel)
{
if (ModelState.IsValid)
{
HttpPostedFileBase file = Request.Files["ImageData"];
viewmodel.Image = ConvertToByte(file);
db.YourDbContextSet.Add(viewmodel);
db.SaveChanges();
}
}

希望对你有帮助

关于httppostedfilebase - 在 SQL 中将 httppostedfilebase 另存为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12463450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com