I have a controller that uses HttpPostedFileBase (.jpg or .png, etc.).
public ActionResult SaveImage(HttpPostedFileBase ImageData)
{
}
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) {
}
});
: , 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), .