gpt4 book ai didi

c# - 如何使用 C# 和 ASP.NET MVC 插入从表单中获取的图像并将其作为字节存储到数据库中

转载 作者:太空宇宙 更新时间:2023-11-03 14:42:29 24 4
gpt4 key购买 nike

我想将一些包括图像(以字节为单位)的数据插入到数据库中。我该怎么做?

Stream fs = img.ImageFile.PostedFile.InputStream;

BinaryReader br = new BinaryReader(fs);

byte[] bytes = br.ReadBytes((Int32)fs.Length);

以上是我试过的

最佳答案

获取文件字节

byte[] file = new byte[img.ImageFile.PostedFile.ContentLength];

您的查询(作为模板)

insert into table file values @file
new SqlParameter("@file",file)

新代码

[HttpPost]
public ActionResult addimage(HttpPostedFileBase ImageFile)
{
byte[] file = new byte[ImageFile.ContentLength];
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("dbo.addimage", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@tname", file);
con.Open();
status = cmd.ExecuteNonQuery();
}
}
}

关于c# - 如何使用 C# 和 ASP.NET MVC 插入从表单中获取的图像并将其作为字节存储到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56093618/

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