Solved

Image retireval in web matrix

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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