mercredi 25 février 2015

How do I display an image in browser using a web server in C using fread?

I am able to display html and plain text file in the browser using my web server.c. However, when I try to load an image jpeg file, it give me this error "The image http://ift.tt/1wdyFIH" cannot be displayed because it contains error.


The content-type i have set it as "image/jpeg".


This is my code:



FILE *fp;
char ch;
unsigned long fileLen;
unsigned char *buffer;

fp = fopen(page+1,"rb");
fseek(fp, 0, SEEK_END);
fileLen=ftell(fp);
fseek(fp, 0, SEEK_SET);
buffer=(char *)malloc(fileLen);
fread(buffer,fileLen,sizeof(unsigned char),fp);

fclose(fp);

write (connection_fd, buffer, strlen(buffer));


This is the header for my image that i have set


"HTTP/1.1 200 OK\r\n" "Content-type: image/jpeg\r\n" "Connection: close\r\n" "\r\n";


Please kindly advise. Thank you :)


Aucun commentaire:

Enregistrer un commentaire