简体   繁体   中英

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]}.

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):

[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.


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:

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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