开发者

How to display image in js?

I store my images in sqlserver database. In my data base I have fields Id, Name, Image. I try to save image:

            Image imag = Image.FromFile(filePath);
        byte[] imageData;
        using (MemoryStream ms = new MemoryStream())
        {
           开发者_如何学Python imag.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            imageData = ms.ToArray();
        }
        var dc = new ImagesDataContext();
        dc.Images.InsertOnSubmit(new Image() { Id = 125, Image1 = imageData, Name = "testIm" });

In my generic handler I ruturn json objec with collection of Images:

JavaScriptSerializer serializer = new JavaScriptSerializer();
        context.Response.ContentType = "application/json";
        List<Image> listOfImages = DatabaseAccessor.GetImages();
        context.Response.Write(serializer.Serialize(listOfImages));

How can I display images on my page using javascript?


Assuming that you chose the datatype Image; you can create at the server end, use Response.BinaryWrite(listOfImages(0).Image), use JavaScript to display the picture.

Something like following within your javascript:

Response.ContentType = "image/jpeg";
Response.BinaryWrite('<%= listOfImages[0].Image %>');


You can create them in the server and show/hide in javascript...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜