help in asp.net C#

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
anyone?????

I will be advising from a language/platform agnostic approach.

Normally you will need a handler or script that will just produce image response back to the browser

The mime-type of the response should be "image/*", where '*' can be gif/jpg/png depending on the type of the image. Hence you normally need to store the mime-type of the image into the database, unless you are dealing with one fileformat of image only, or you like to parse the image everytime to find out the type of the image.

So you just print out the headers of the image, follow by the binary body of the image

When you refer such scripted image handler in your HTML pages, just give it an identifier to fetch the image from the database.
Code:
<img src="/image_handler?id=12345"></img>

Alternatively you can also use Data URI scheme, which you can read up from https://en.wikipedia.org/wiki/Data_URI_scheme. That doesn't require a separate round trip for the browser to fetch the image resource, but it doesn't necessarily means faster because your main page may have longer latency to produce the html page with the data of the image embedded. Also base64 is not exactly an efficient encoding. Worse is IE didn't support it until V8.0 and still not fully supported as seen in Can I use Data URIs

I do see recent frequent usage of data uri scheme in a lot of examples online though.

Below is an incomplete example on how it can be done in perl
Code:
#!/usr/bin/perl

use strict;
use warnings;

my $id = ... # get from CGI request
eval {
  my $rv = $sth->execute("SELECT mimetype, data FROM img where id=?", $id) or die($sth->errstr)
  my($mimetype,$data) = $sth->fetchrow_array();
  my $length = length($data);
  
  print <<HEADERS;
Content-Type: $mimetype
Content-Length: $length

$data
HEADERS
};
if ($@) {
  print "Status: 404 $@ \n\n";
}

Reminder. This approach is a naive approach since you will run into performance issue very quickly. Normally the correct approach is at least client-side caching should be use to avoid unnecessarily network load, next is server-side caching should be used too avoid database load. Server side caching can be implemented in memory or as temporary file. You may also use memcache or any other caching approaches.
 
Last edited:
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top