Solved

Image retireval in web matrix

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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