|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Outputting file through web browser
Ok, so I'm making a script that connects to the database, get's the file info from the db (path & file name), it gets the mime type form the database too.
Now the problem is sending that info to the web browser so they can view movies through Windows Media/IE. Here's the code I can't get right (full code below): Code:
/* FILE VARIABLES */
char filename[255];
FILE *videofile;
char buffer[1024];
int videosize = atoi(row[3]);
/* SEND HEADERS */
printf("Content-Type: %s\n",row[2]);
printf("Content-Length: %i\n\n",videosize);
/* BULID FULL FILENAME FROM DB ROW */
sprintf(filename,"%s/%s",row[1],row[0]);
mysql_free_result(res);
mysql_close(conn);
videofile = fopen(filename,"r");
while(fgets(buffer,1024,videofile)!=NULL) printf("%s",buffer);
fclose(videofile);
free(buffer);
The full code: Code:
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <getval.h>
main (int argc, char *argv[]) {
/* SET MYSQL VARIABLES */
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char sqlbuff[255];
/* SET MYSQL CONNECTION VARIABLES */
char *server = "dbserver";
char *user = "username";
char *password = "password";
char *database = getval("dbname");
/* SET VIDEOID VARIABLE */
char *str1 = getval("videoid");
int VideoId = atoi(str1);
conn = mysql_init(NULL);
/* CONNECT TO MYSQL DATABASE */
if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
printf("Content-type: text/html\n\n");
fprintf(stderr, "%s\n", mysql_error(conn));
exit(0);
}
/* Do QUERY */
sprintf(sqlbuff, "SELECT FileName,FilePath,MimeType,FileSize FROM ContentAV WHERE Id = \'%i\'", VideoId);
if(mysql_query(conn, sqlbuff)) {
printf("Content-type: text/html\n\n");
fprintf(stderr, "%s\n", mysql_error(conn));
exit(0);
}
res = mysql_use_result(conn);
row = mysql_fetch_row(res);
/* FILE VARIABLES */
char filename[255];
FILE *videofile;
char buffer[1024];
int videosize = atoi(row[3]);
/* SEND HEADERS */
printf("Content-Type: %s\n",row[2]);
printf("Content-Length: %i\n\n",videosize);
/* BULID FULL FILENAME FROM DB ROW */
sprintf(filename,"%s/%s",row[1],row[0]);
mysql_free_result(res);
mysql_close(conn);
videofile = fopen(filename,"r");
while(fgets(buffer,1024,videofile)!=NULL) printf("%s",buffer);
fclose(videofile);
free(buffer);
}
Thanks for any help! I've been sitting here for a while ![]() |
|
#2
|
||||
|
||||
|
> char buffer[1024];
> free(buffer); You can't free what you didn't malloc.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour |
|
#3
|
|||
|
|||
|
Quote:
I'm still very lost on this. For some reason it just isn't working right. It gets to the last few hundred bites and the download stops, or sometimes it only get's half way. New code: Code:
/* FILE VARIABLES */
char filename[255];
FILE *videofile;
int buffersize = 2;
char buffer[buffersize];
int videosize = atoi(row[3]);
/* SEND HEADERS */
printf("Content-Type: %s\n",row[2]);
printf("Content-Disposition: attachment; filename=%s\n",row[0]);
printf("Content-Length: %i\n\n",videosize);
/* BULID FULL FILENAME FROM DB ROW */
sprintf(filename,"%s/%s",row[1],row[0]);
mysql_free_result(res);
mysql_close(conn);
videofile = fopen(filename,"rb");
/* while(fgets(buffer,1024,videofile)!=NULL) printf("%s",; */
int i=0;
while(i <= videosize) {
fgets(buffer, buffersize, videofile);
fputs(buffer, stdout);
i=i+buffersize;
}
fclose(videofile);
|
|
#4
|
|||
|
|||
|
You call it videofile but treat it like a text-file.
Code:
int i=0;
while(i <= videosize) {
fgets(buffer, buffersize, videofile); // read a line of text
fputs(buffer, stdout); // print that line. Is it really text ?
i=i+buffersize; // add buffersize
// but that buffer is most propably not filled completly
// fgets stops at newline
}
Guess you have to read the file in binary mode and use fread(). Looks your are trying to output to stdout because the webserver reads from stdin. I have never tried but I think you will have to switch stdout to binary mode as well. I might be wrong, fwrite() could work. Kurt |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Outputting file through web browser |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|