I´m using Kayak server for a small project. It works great, but i have a problem when i have a request for a png or jpg image
First of all the code is here:
public class RequestDelegate : IHttpRequestDelegate{
*****
public void OnRequest(HttpRequestHead request, IDataProducer requestBody,IHttpResponseDelegate response){
*****
else if (url.EndsWith("png"))
{
String fileName = url.Substring(url.IndexOf(@"\") + 1);
url = url.Substring(url.IndexOf(@"\") + 1);
url = Path.Combine(WebServerUtils.Instance.HtmlURL, url);
using (StreamReader streamReader = new StreamReader(url))
{
String body = streamReader.ReadToEnd();
var responseHeader = WebServerUtils.Instance.CreateResponseHeader(body.Length, "image/png");
responseHeader.Headers.Add("Content-Disposition", "attachment; filename=" + fileName);
response.OnResponse(responseHeader, new BufferedProducer(body));
}
}
****
}
This code is executed when a png image is requested. It’s very simple I locate the real url path of the image, the i read it with a stream reader. Then i create the response header..
this part of code
var responseHeader = WebServerUtils.Instance.CreateResponseHeader(body.Length, "image/png");
here is the implementation
public HttpResponseHead CreateResponseHeader(int length, String type)
{
var _ResponseHeader = new HttpResponseHead()
{
Status = "200 OK",
Headers = new Dictionary<string, string>(){
{ "Content-Type", type },
{ "Content-Length", length.ToString() },
}
};
return _ResponseHeader;
}
Now here is the situation when i ask for the page non of the images are rendered. When i use Firebug to see what happen.. here is the error
Image corrupt or truncated: http://localhost:9090/Pedidos/back.png
Am I missing something when i’m building the response header?
I can not return properly an image..
Thanks in advance hope be clear with the explanation.
Well, it was very simple.. i´m using a Stream reader ( it inheritance from Text Reader).. I must use a memory stream in place of a Stream reader.
Here is the code: