HTML文件的上传与下载
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#include "UpDownFile.h"
#include "cgic.h"
/* LoadFile.c */
int ServerToWebFile( const char * fileName )
{
int result = -1;
char filebuf[MAX_FILE_LEN];
char cmd[65535];
struct stat sb;
sprintf( cmd, "%s%s", DOWNLOAD_FILE_PATH, fileName );
stat( cmd, &sb ); /* 取得下载文件的大小 */
/* 输出HTTP头信息,输出附加下载文件、文件长度以及内容类型 */
printf( "Content-Disposition:attachment;filename=%s", fileName );
printf( "\r\n" );
printf( "Content-Length:%d", (int) sb.st_size );
printf( "\r\n" );
printf( "Content-Type:application/octet-stream\r\n" );
printf( "\r\n" );
sprintf( cmd, "%s%s", DOWNLOAD_FILE_PATH, fileName );
{
FILE *fp = NULL;
fp = fopen( cmd, "r+b" );
if ( fp != NULL )
{
do
{
int rs = fread( filebuf, 1, sizeof(filebuf), fp );
fwrite( filebuf, rs, 1, stdout );
}
while ( !feof( fp ) );
fclose( fp );
}
}
return(result);
}
/*上传文件 */
int WebToServerFile( char* pageID, char* filePath )
{
/* int result = -1; */
cgiFilePtr file;
int targetFile;
mode_t mode;
char name[128];
char fileNameOnServer[64];
char contentType[1024];
char buffer[BufferLen];
char *tmpStr = NULL;
int size;
int got, t;
cgiHeaderContentType( "text/html" );
/* 取得html页面中file元素的值,应该是文件在客户机上的路径名 */
if ( cgiFormFileName( pageID, name, sizeof(name) ) != cgiFormSuccess )
{
fprintf( stderr, "could not retrieve filename\n" );
/* goto FAIL; */
}
cgiFormFileSize( pageID, &size );
/* 取得文件类型,不过本例中并未使用 */
cgiFormFileContentType( pageID, contentType, sizeof(contentType) );
/* 目前文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件。临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件 */
if ( cgiFormFileOpen( pageID, &file ) != cgiFormSuccess )
{
fprintf( stderr, "could not open the file\n" );
return(-1);
/* goto FAIL; */
}
t = -1;
/* 从路径名解析出用户文件名 */
while ( 1 )
{
tmpStr = strstr( name + t + 1, "\\" );
if ( NULL == tmpStr )
tmpStr = strstr( name + t + 1, "/" ); /* if "\\" is not path separator, try "/" */
if ( NULL != tmpStr )
t = (int) (tmpStr - name);
else
break;
}
strcpy( fileNameOnServer, name + t + 1 );
/* 将文件名与文件路径拼接 */
strcat( filePath, fileNameOnServer );
mode = S_IRWXU | S_IRGRP | S_IROTH;
/* 判断上传的文件是否存在,不存在则进行上传 */
if ( access( filePath, 0 ) == 0 ) /* 判断存在时第二个参数给0,若存在或者具有权限,返回值为0;不存在或者无权限,返回值为-1。 */
{
/* 存在则返回错误提示 */
fprintf( stderr, "the file existed!", filePath );
return(-1);
}else {
/*不存在,在filePath目录下建立新的文件 */
targetFile = open( filePath, O_RDWR | O_CREAT | O_TRUNC | O_APPEND, mode );
if ( targetFile < 0 )
{
fprintf( stderr, "could not create the new file,%s\n", filePath );
return(-1);
/* goto FAIL; */
}else {
/* 从系统临时文件中读出文件内容,并放到刚创建的目标文件中 */
while ( cgiFormFileRead( file, buffer, BufferLen, &got ) == cgiFormSuccess )
{
if ( got > 0 )
write( targetFile, buffer, got );
else{
break;
}
}
printf( "File \"%s\" has been uploaded", filePath );
}
/*
* goto END;
* FAIL: fprintf(stderr, "Failed to upload");
* return 1;
* END:
*/
close( targetFile );
}
cgiFormFileClose( file );
return(0);
}
/*
* 查找指定目录的文件
* 自定义的过滤器函数
* 本函数只能做为 scandir 函数的参数,被 scandir 函数回调
* 注:1. 本函数不能被定义为 static int custom_filter( const struct dirent *pSDirent ) 静态函数;
* 2. 参数一定要为 const struct dirent *pSDirent 不可省略前面的 const ;
*/
/*int custom_filter( const struct dirent *dp)
* {
* if ( 0 == strncmp("get", dp->d_name,3 ) )
* {
* // 非零为保留 - 不滤掉
* return 1;
* }
* // 零为过滤掉 - 过滤掉
* return 0;
* }*/
int fileFind( const char* filePath )
{
struct dirent **namelist;
int i;
int total;
/* 返回filePath目录下满足过滤条件的文件,并按照alphasort函数的方式,存放到namelist中,这里未指明过滤条件 */
total = scandir( filePath, &namelist, 0 /*custom_filter*/, alphasort );
if ( total < 0 )
{
printf( "scandir fault\n" );
}else {
for ( i = 0; i < total; i++ )
{
printf( "%s\n", namelist[i]->d_name );
free( namelist[i] );
}
printf( "total = %d\n", total );
}
free( namelist );
return(0);
}