Unable to convert HttpPostedFileBase to Byte []

I have a controller that uses HttpPostedFileBase (.jpg or .png, etc.).

public ActionResult SaveImage(HttpPostedFileBase ImageData)
{
  //code
}

ImageDatabecomes an object System.Web.HttpPostedFileWrapperwith these properties:

ContentLength: 71945
ContentType: "image/png"
FileName: "foo.png"
InputStream: {System.Web.HttpInputStream}

I have no problem getting ImageData and converting it to an image and then converting the image to byte [] and then to base64 string, but I tried to convert it directly to byte [] using the following code:

byte[] imgData;

using (Stream inputStream = ImageData.InputStream)
{
    MemoryStream memoryStream = inputStream as MemoryStream;
    if (memoryStream == null)
    {
        memoryStream = new MemoryStream();
        inputStream.CopyTo(memoryStream);
    }

    imgData = memoryStream.ToArray();
}

memoryStreamIt is always empty at the time of launch imgData = memoryStream.ToArray();, therefore imgDatait also ends with zero.

, InputStream MemoryStream. , InputStream , readTimeout writeTimeout, timeouts are not supported on this stream. , ImageData []?

, AJAX. contentType processData, false?

$.ajax({
    url: 'SaveImage',
    data: formData,
    type: "POST",
    contentType: false,
    processData: false,
    beforeSend: function () {
        $("#loadingScreenModal").modal('toggle');
    },
    success: function (data) {
        // etc.
    }
});

: , HttpPostedFileBase Image, Image byte[], .

Image i = Image.FromStream(ImageData.InputStream, true, true);
byte[] imgData = imageToByteArray(thumb);

public byte[] imageToByteArray(Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

# 2: , , , , - if (memoryStream == null), .

+4
4

memoryStream == null, JoeEnos. MemoryStream - - if (memoryStream.Length == 0).

+2

BinaryReader :

byte[] imgData;

using (var reader = new BinaryReader(ImageData.InputStream))
{
    imgData = reader.ReadBytes(ImageData.ContentLength);
}
+5

, [] :

var streamLength = ImageData.InputStream.Length;
var imageBytes = new byte[streamLength];
ImageData.InputStream.Read(imageBytes, 0, imageBytes.Length);

imageBytes varbinary(MAX). , .

+2

public class AdminDetailsModel
{
    public byte[] AdminImage { get; set; }
 }

@using (Html.BeginForm("Create", "AdminDetails", FormMethod.Post, new { enctype = "multipart/form-data" }))
{   
    <div class="form-group">
        @Html.LabelFor(model => model.AdminImage, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="ImageData" id="ImageData" />                              
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>

,

public ActionResult Create(AdminDetailsModel viewmodel)
{
    if (ModelState.IsValid)
    {
    HttpPostedFileBase file = Request.Files["ImageData"];
    viewmodel.Image = ConvertToByte(file);
    db.YourDbContextSet.Add(viewmodel);
    db.SaveChanges();
    }
}

public byte[] ConvertToByte(HttpPostedFileBase file)
    {
        byte[] imageByte = null;
        BinaryReader rdr = new BinaryReader(file.InputStream);
        imageByte = rdr.ReadBytes((int)file.ContentLength);
        return imageByte;
    }

,

+1

All Articles