Solved

Image retireval in web matrix

Posted on 2011-09-12
3
519 Views
Last Modified: 2012-05-12
Hey guys, I am having srious issues trying to get the image out of my db. I am trying to create a sort of blog layout so i am using divs and a @foreach so each post looks the same I have the data and image in the same table.  here is my insert code

@{
   var db = Database.Open("Renovoblog");
   var BlogCat = Request ["BlogCat"];
   var BlogContent = Request ["BlogContent"];
   var BlogTitle = Request ["BlogTitle"];
    
    if (IsPost){
        
        BlogCat = Request ["BlogCat"];
        if (BlogCat.IsEmpty ()){
           ModelState.AddError ("BlogCat", "Needed");          
        }
        BlogContent = Request ["BlogContent"];
        if (BlogContent.IsEmpty ()){
           ModelState.AddError ("BlogContent", "Needed");           
        }
        BlogTitle = Request ["BlogTitle"];
        if (BlogCat.IsEmpty ()){
           ModelState.AddError ("BlogTitle", "Needed");            
        }
        
         var numFiles = Request.Files.Count;
        for (int i = 0; i < numFiles; i++) {
            var file = Request.Files[i];
            if (file.ContentLength > 0) {
                var fileUpload = new WebImage(file.InputStream);
                var fileTitle = Path.GetFileNameWithoutExtension(file.FileName).Trim();
                if (fileTitle.IsEmpty()) {
                    fileTitle = "Untitled";
                }
                var fileExtension = Path.GetExtension(file.FileName).Trim();
                var fileBytes = fileUpload.GetBytes();
        
        if (ModelState.IsValid){
            var insertQuery = " INSERT INTO Post (BlogCat, BlogContent, BlogTitle, FileTitle, FileExtension, ContentType, FileSize, FileContents)" + "VALUES (@0,@1,@2,@3,@4,@5,@6,@7)";
            db.Execute(insertQuery, BlogCat, BlogContent, BlogTitle, fileTitle, fileExtension, fileUpload.ImageFormat, fileBytes.Length, fileBytes);
             Response.Redirect(@Href("Postconfirm"));
        }}
            
        }}}

Open in new window


And thid is the retrival code  

@{
   int id = 0;    
    var db = Database.Open("Renovoblog");
    var selectQueryString = "SELECT * FROM Post ORDER BY BlogID"; 
      
    
}
}
<!--Blog Post -->
    
@foreach(var row in db.Query(selectQueryString)){
<div class="box post" id="post-41">
<div class="content">
 <div class="left_sise">
<!--Image with Jqueary Doc -->
<div class="pic">  
    <!--PICTURE NEEDS TO GO HERE -->
</div>
<div class="social-links">
<!--/post-date -->
<div class="post-date">@row.BlogDate</div>
<div class="clr"></div>
</div>
</div>   
 <!--Rightside of post -->   
<div class="right_sise">

<div class="post-title">
<h2>@row.BlogTitle</h2></div>
<div class="post-excerpt">
<p>@row.BlogContent</p>
</div>
</div>
</div>
<div class="clr"></div>
</div>
<div class="bg"></div>
    }

Open in new window


Where you see the   <!--PICTURE NEEDS TO GO HERE -->    thats where i need it to retrieve the image

Thank you in advance  
0
Comment
Question by:keeffe
3 Comments
 
LVL 8

Accepted Solution

by:
SunnyDark earned 500 total points
Comment Utility
You cannot directly display image bytes from DB on HTML page , you have 2 alternatives here:
Option 1:
Either a server side function that will read the bytes from DB and write them into the response stream, ie:

<img src="some_asp_net_page.aspx?imageid=123456"/>

Then in some_asp_net_page.aspx Page_Load event:

void Page_Load ()
{
   var imageBytes = // read the bytes of imageid=123456 from DB
   Response.OutputStream.Write(imageBytes);
}

Open in new window




Option 2:
Use a data url , that is save your image in the database as data url Data URL
This way it can be displayed directly from the database
0
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
Comment Utility
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now