Solved

Image retireval in web matrix

Posted on 2011-09-12
3
528 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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