开发者

How-to: Byte array of an image to string, send back through HTTP request, then back to byte[] image?

I have a byte array of an image on the server. Using MVC as a pseudo-REST web service interface.

I'm in need of sending this image back through the HTTP request to the MVC client to render.

My first attempt was using UFT8Encoding to encode it to a string, send that cross then decode it using UTF8Encoding on the client. However, when I do this, the result on the client is null. I assume due to the format of the string that I'm trying to send back.

This is what I'm doing now to no avail:

        byte[] image = GetBarcodeImage(barcode);
        if (image != null)
        {
            UTF8Encoding enc = new UTF8Encoding();
            result = enc.GetString(image);
        }

This is on the client side:

        UTF8Encoding  encoding= new UTF8Encoding();
        byte[] image = encoding.GetBytes(result);
        string imageBase开发者_开发百科64 = Convert.ToBase64String(image);
        string imgsrc = string.Format("data:image/gif;base64,{0}", imageBase64);


Is there any reason you don't return it as an image from your controller?

var image = GetByteArrayImage();
var stream = new MemoryStream(image);
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Expires = 14400;
return File(stream, "image/jpg");

Thin in your client you can just use WebRequest to download the file like any other file and not have to do any other magic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜