我正在尝试将图像插入数据库表中,但是在选择图像后,上载的图像在表中显示为NULL。帮助我将图像正确上传到数据库中。
这是我的将数据插入数据库表的代码:
protected void addProduct_Click(object sender, EventArgs e)
{
using (TradersRehmanEntities db = new TradersRehmanEntities())
{
tblProduct ad = new tblProduct();
ad.ProductName = txtProducttName.Text;
ad.ProductPrice = txtSalePrice.Text;
ad.SalePrice = txtSalePrice.Text;
ad.StockAvaliable = txtStock.Text;
ad.CategoriesId = Convert.ToInt32(catergoriesDDL.SelectedValue);
ad.ProductImage = flImageUpload.SaveAs('ProductImage' + flImageUpload.FileName);
db.tblProducts.Add(ad);
db.SaveChanges();
Response.Write("Product added successfully");
}
}
[尝试将其另存为blob。但是最好将其保存在对象存储区(例如Amazon S3)中,并在db中保留对其的引用
尝试一下-从.FileBytes
控件的FileUpload
属性中获取上载文件的字节数组:
protected void addProduct_Click(object sender, EventArgs e)
{
using (TradersRehmanEntities db = new TradersRehmanEntities())
{
tblProduct ad = new tblProduct();
ad.ProductName = txtProducttName.Text;
ad.ProductPrice = txtSalePrice.Text;
ad.SalePrice = txtSalePrice.Text;
ad.StockAvaliable = txtStock.Text;
ad.CategoriesId = Convert.ToInt32(catergoriesDDL.SelectedValue);
// grab the file's byte array from the FileUpload control
ad.ProductImage = flImageUpload.FileBytes();
db.tblProducts.Add(ad);
db.SaveChanges();
Response.Write("Product added successfully");
}
}
您可以将图像作为字节数组存储在数据库中。