我无法将图像存储到数据库中 [英] i am unable to store image into database

查看:85
本文介绍了我无法将图像存储到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[HttpPost]
public ActionResult Registration(FormCollection obj_frmRegistration)
{
    objc.AdminName = obj_frmRegistration["AdminName"];
    objc.Gender = obj_frmRegistration["Gender"];
    objc.ImageUrl = obj_frmRegistration["Image"];

    using (var stream = new MemoryStream())
    {
        objc.ImageUpload.InputStream.CopyTo(stream);
        byte[] photo = stream.ToArray();
    }





转换后应存储的图像

objc.profileimage。



个人资料图片类型为byte



after converting that image it should store in
objc.profileimage.

profile image is of type byte

推荐答案

这不是从视图中获取图像的好方法。使用 HttpPostedFileBase [ ^ ]。

一个例子是 -

控制器

This is not a good way to get the image from the view. Use HttpPostedFileBase[^].
An example would be-
Controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(PhotoForSingleItem photo)
{
  PhotoViewImage newImage = new PhotoViewImage();
  HttpPostedFileBase file = Request.Files["OriginalLocation"];
  newImage.Name = photo.Name;
  newImage.Alt = photo.AlternateText;
  newImage.ContentType = file.ContentType;

  Int32 length = file.ContentLength;
  byte[] tempImage = new byte[length];
  file.InputStream.Read(tempImage, 0, length);
  newImage.ActualImage = tempImage ;

  newImage.Save();

  return View();
}



查看


View:

<form method="post" enctype="multipart/form-data" action="Photo/Upload">
  <div>
    <span>
     Name:
   </span>
   <span>
     <input type="text" id="Name" name="Name" />
   </span>
  </div>
  <div>
    <span>
      Alternate Text:
    </span>
    <span>
     <input type="text" id="AlternateText" name="AlternateText" />
    </span>
  </div>
  <div>
    <span>
      Image
    </span>
    <span>
      <input type="file" id="OriginalLocation" name="OriginalLocation" />
    </span>
  </div>
  <div>
    <input type="submit" value="Upload" />
  </div>
</form>





请参阅此处的完整教程 -

ASP.Net MVC:使用视图动态地将图像上传到数据库并显示图像[ ^ ]



-KR



See the complete tutorial here-
ASP.Net MVC: Upload Image to Database and Show Image "Dynamically" Using a View[^]

-KR


这篇关于我无法将图像存储到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆