简体   繁体   English

无法将上传的图像转换为字节数组

[英]unable to convert uploaded image to byte array

I am trying convert an uploaded image to a byte array so that I can store it in a database table. 我正在尝试将上传的图像转换为字节数组,以便可以将其存储在数据库表中。

The code below is used to perform the conversion from an image into a byte array: 以下代码用于执行从图像到字节数组的转换:

public byte[] ConvertToBytes(HttpPostedFileBase image)
{
     BinaryReader reader = new BinaryReader(image.InputStream);
     var imageBytes = reader.ReadBytes((int)image.ContentLength);
     return imageBytes;
}

when I place breakpoints on this code to see what is being returned the imageBytes variable displays {byte[0]}. 当我在此代码上放置断点以查看返回的图像时,imageBytes变量显示{byte [0]}。

the code shown below is the receiving ActionResult in the controller for the view I am using to upload this image (currently I am using a file input to select and upload the image): 下面显示的代码是我正在用于上传该图像的视图的控制器中的ActionResult接收操作(当前,我正在使用文件输入来选择和上传图像):

[HttpPost]
public ActionResult NewsManager(NewsManagerViewModel model)
{
    var newsManagerRepository = new NewsManagerRepository();
    var currentUser = User.Identity.Name;

    if (ModelState.IsValid)
    {
        HttpPostedFileBase file = Request.Files["ImageData"];

        var fileIsImage = file.IsImage();

        if (fileIsImage)
        {
            model.Author = currentUser;

            var newsUploaded = newsManagerRepository.UploadNews(file, model);

            if (newsUploaded == 1)
            {
                return View();
                }

                ModelState.AddModelError("uploadFailed", "News item was not uploaded");

                return View(model);
                }
                ModelState.AddModelError("fileNotImage", "the file you have uploaded is not an image");

                return View(model);
            }

            return View(model);
        }

does anyone have any ideas as to why the images I am converting are not being successfully converted to a byte array? 有谁对我要转换的图像为何未成功转换为字节数组有任何想法?

Any suggestions would be very much appreciated, the application is currently MVC 5 and .net version 4.5. 任何建议将不胜感激,该应用程序当前是MVC 5和.net版本4.5。


the calling method code is below: 调用方法代码如下:

public int UploadNews(HttpPostedFileBase file, NewsManagerViewModel model)
{
    model.BannerImage = ConvertToBytes(file);
    var ndtms2Utils = new NDTMS2UtilsEntities();

    var news = new News
    {
        Title = model.Title,
        Author = model.Author,
        BannerImage = model.BannerImage,
        DateCreated = DateTime.Now,
        NewsContent = model.NewsContent
    };

    ndtms2Utils.News.Add(news);
    int i = ndtms2Utils.SaveChanges();
    if (i == 1)
    {
        return 1;
    }
    return 0;
}

Use the convert method as mentioned below: 使用如下所述的convert方法:

public byte[] ConvertToBytes(HttpPostedFileBase image)
{
   return image.InputStream.StreamToByteArray();
}

public static byte[] StreamToByteArray(this Stream input)
{
    input.Position = 0;
    using (var ms = new MemoryStream())
    {
        int length = System.Convert.ToInt32(input.Length);
        input.CopyTo(ms, length);
        return ms.ToArray();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM