Solved

Image retireval in web matrix

Posted on 2011-09-12
3
527 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Accepted Solution

by:
SunnyDark earned 500 total points
ID: 36896467
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
ID: 37909735
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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!
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

738 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