一切正常,但一行出现问题 当我传递文件System.Drawing.Image imageToBeResize = System.Drawing.Image.FromFile(file)时,下面的行给了我错误。 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我必须向数据库中添加图像我的数据库如下
TableName = Product
- PId[int]
- PImage[nvarchar(max)]
我正在使用DB FIRST APPROACH在ASP.NET MVC中创建应用程序,因此我的模型类如下(自动生成)
using System;
using System.Collections.Generic;
public partial class Product
{
public int PId { get; set; }
public string PImage { get; set; }
}
我的用于添加新图像的控制器如下。 我这样做是因为我的观点在React中
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(string myData)
{
var details = Newtonsoft.Json.Linq.JObject.Parse(myData);
int PId = Convert.ToInt32((string)details["PId"]);
string PImage = [(string)details["PImage"]]; // An error is coming Cannot implicitly convert type 'System.Web.HttpPostedFileBase' to 'string'
Product p = new Product() { PId = PId, PImage = PImage };
db.Products.Add(p);
db.SaveChanges();
return View();
}
我的观点很好,它同时返回了PId和PImage PId正在保存在db中。问题是我不知道如何在db中保存图像。
添加课程:
public HttpPostedFileBase Image { get; set; }
然后控制器:
var imageFile = ConvertFileInByteArray(model.Image.InputStream, model.Image.ContentLength);
private byte[] ConvertFileInByteArray(Stream inputStream, int contentLength)
{
try
{
byte[] file = null;
using (var binaryReader = new BinaryReader(inputStream))
{
file = binaryReader.ReadBytes(contentLength);
}
return file;
}
catch (Exception e)
{
_logger.Error(e);
Console.Write(e.ToString());
throw;
}
}
string imageStr = ComputeHash(imageFile);
private string ComputeHash(byte[] file)
{
MD5 md5 = MD5.Create();
byte[] hashAraay = md5.ComputeHash(file);
var builder = new StringBuilder();
foreach (byte b in hashAraay)
{
builder.AppendFormat("{0:x2}", b);
}
return builder.ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.