tu peux utiliser un httphandler.
exemple pour lire une image depuis le server (ok, c'est con, mais c'est pour l'exemple, la lecture peut se faire depuis une BD ensuite)
déclaration du handler dans le web.config
1 2 3
| <httpHandlers>
<add verb="*" path="displayimg" type="demoImgFromMemory.HttpHandlerImage,demoImgFromMemory"/>
</httpHandlers> |
Handler :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| using System;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
namespace demoImgFromMemory
{
public class HttpHandlerImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
HttpServerUtility server = context.Server;
string file = request.QueryString["file"];
string filePath = server.MapPath(file);
if (!string.IsNullOrEmpty(file) && File.Exists(filePath))
{
System.Drawing.Image img = System.Drawing.Image.FromFile(filePath);
string contentType;
ImageFormat format;
switch(Path.GetExtension(file).ToUpper())
{
case ".GIF":
contentType = "image/gif";
format = ImageFormat.Gif;
break;
case ".JPG":
contentType = "image/jpeg";
format = ImageFormat.Jpeg;
break;
case ".PNG":
contentType = "image/png";
format = ImageFormat.Png;
break;
case ".BMP":
contentType = "image/bmp";
format = ImageFormat.Bmp;
break;
default:
throw new NotSupportedException("Type d'image non reconnu");
}
context.Response.ContentType = contentType;
img.Save(context.Response.OutputStream, format);
}
else
{
context.Response.StatusCode = 404;
}
}
public bool IsReusable
{
get { return true; }
}
}
} |
fichier aspx
<img src="displayimg?file=img.jpg" />
Partager