mercredi 20 décembre 2017

How to send image over HTTP/1.1 from server side to client browser?

so i am trying to send html, jpg and gif from server to browser upon request. The issue is, when i try to send the image, gibberish text is being printed on the browser.

Here's what i'm trying

if (ext == ".jpg")
 {
     byte[] ar;
     FileStream fstream;

     using (fstream = new FileStream("dp.jpeg", FileMode.Open, FileAccess.Read))
     {
        ar = new byte[fstream.Length];
        fstream.Read(ar, 0, ar.Length);
     }

     sw.WriteLine("Content-Type: image\\jpeg");
     sw.WriteLine("Content-Length: {0}", ar.Length);
     sw.WriteLine();
     sw.BaseStream.Write(ar, 0, ar.Length);
 }

I am totally new to this HTTP and webserver stuff so pardon any stupid mistakes.




Aucun commentaire:

Enregistrer un commentaire