分享一点代码(小型C web开发框架)共11篇章 (含全部源代码),范例:用C语言实现的一个WEB 文件上传

itbusinessproces 2011-06-16 07:26:40
分享一点代码(小型C web开发框架)共11篇章 (含全部源代码),范例:用C语言实现的一个WEB 文件上传
在 windows IIS Apache linux Apache IE Firefox 均测试通过
编译:用Vs2008和GCC 均可编译
共11篇章 (含全部源代码),欢迎各位专家,高手指正!

#include "cgic.h"

#define BufferLen 1024
int cgiMain(void){
int i = 0;
char fn[1024];
char type[1024];
char name[1024];
char **responses;

cgiFilePtr file;
FILE *fp;
int targetFile;
char fileNameOnServer[64];
char contentType[1024];
char buffer[BufferLen];
char *tmpStr=NULL;
int size;
int got,t;
cgiHeaderContentType("text/html");
fprintf(cgiOut, "<title>C WEB</title>\n");
fprintf(cgiOut, "<h1>C语言开发</h1>\n");

fprintf(cgiOut, "<br>cgiServerSoftware %s \n", cgiServerSoftware);
fprintf(cgiOut, "<br>cgiServerName %s \n", cgiServerName);
fprintf(cgiOut, "<br>cgiGatewayInterface %s \n", cgiGatewayInterface);
fprintf(cgiOut, "<br>cgiServerProtocol %s \n", cgiServerProtocol);
fprintf(cgiOut, "<br>cgiServerPort %s \n", cgiServerPort);
fprintf(cgiOut, "<br>cgiRequestMethod %s \n", cgiRequestMethod);
fprintf(cgiOut, "<br>cgiPathInfo %s \n", cgiPathInfo);
fprintf(cgiOut, "<br>cgiPathTranslated %s \n", cgiPathTranslated);
fprintf(cgiOut, "<br>cgiQueryString %s \n", cgiQueryString);
fprintf(cgiOut, "<br>cgiRemoteHost %s \n", cgiRemoteHost);
fprintf(cgiOut, "<br>cgiRemoteAddr %s \n", cgiRemoteAddr);
fprintf(cgiOut, "<br>cgiAuthType %s \n", cgiAuthType);
fprintf(cgiOut, "<br>cgiRemoteUser %s \n", cgiRemoteUser);
fprintf(cgiOut, "<br>cgiRemoteIdent %s \n", cgiRemoteIdent);
fprintf(cgiOut, "<br>cgiContentType %s \n", cgiContentType);
fprintf(cgiOut, "<br>cgiAccept %s \n", cgiAccept);
fprintf(cgiOut, "<br>cgiUserAgent %s \n", cgiUserAgent);
fprintf(cgiOut, "<br>cgiReferrer %s \n", cgiReferrer);

cgiFormString( "fn", fn , sizeof( fn ) );
cgiFormString("type", type, sizeof( type ));
cgiFormString("name", name, sizeof( name ));
fprintf( cgiOut , "<br/>fn:" );
fprintf( cgiOut , fn );

fprintf( cgiOut , "<br/>type:" );
fprintf( cgiOut , type );

fprintf( cgiOut , "<br/>name:" );
fprintf( cgiOut , name );

if(strcmp(cgiRequestMethod, "GET")==0)
{
fprintf(cgiOut, "<form target='_blank' method='post' enctype='multipart/form-data' > ");
fprintf(cgiOut, " <input name='file' id='file' type='file' /> <input name='submit' type='submit' /> ");
fprintf(cgiOut, "</form>");
}
else
{
if (cgiFormFileName("file", name, sizeof(name)) !=cgiFormSuccess) {
fprintf(stderr,"could not retrieve filename\n");
goto FAIL;
}
cgiFormFileSize("file", &size);
cgiFormFileContentType("file", contentType, sizeof(contentType));

if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
fprintf(stderr,"could not open the file\n");
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);
if((fp=fopen(fileNameOnServer, "wb+"))==NULL)
{
fprintf(stderr,"could not create the new file,%s\n",fileNameOnServer);
goto FAIL;
}

//if( (fp = fopen(fileNameOnServer, "rb+"))==NULL) ){
// fprintf(stderr,"could not create the new file,%s\n",fileNameOnServer);
// goto FAIL;
//}
//Read the content from the tmp file, and write it into the newly created file.
while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){
if(got>0)
fwrite(&buffer, BufferLen, 1, fp);
//write(file,buffer,got);
//write(fp,buffer,got);
}
cgiFormFileClose(file);
fclose(fp);
goto END;
FAIL:
fprintf(stderr,"Failed to upload");
return 1;
END:
fprintf(cgiOut, "<form target='_blank' method='post' enctype='multipart/form-data' > ");
fprintf(cgiOut, " <input name='file' id='file' type='file' /> <input name='submit' type='submit' />");
fprintf(cgiOut, "</form><br/>");

printf("恭喜您!File \"%s\" has been uploaded",fileNameOnServer);
printf("<br/><img src=\"/dialog/%s\" />",fileNameOnServer);
return 0;
}
}

限于篇幅其余代码请下载地址:C语言Web开发框架代码下载

...全文
218 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
oneatree 2011-07-02
  • 打赏
  • 举报
回复
mark
evilc00l 2011-06-19
  • 打赏
  • 举报
回复
为什么不提供开发文档?
Riquelme1O 2011-06-18
  • 打赏
  • 举报
回复
Mark!
fly_t0_the_winds 2011-06-17
  • 打赏
  • 举报
回复
支持,支持
bcccc10 2011-06-17
  • 打赏
  • 举报
回复
超强,收藏
zhang6yinting 2011-06-17
  • 打赏
  • 举报
回复
确实不错
a5lcrack 2011-06-16
  • 打赏
  • 举报
回复
很好,很强大
one_1_one_1 2011-06-16
  • 打赏
  • 举报
回复
飞机中的战斗机啊
aweb123abc 2011-06-16
  • 打赏
  • 举报
回复
希望提供更多范例,谢谢!

2,760

社区成员

发帖
与我相关
我的任务
社区描述
搜索引擎的服务器通过网络搜索软件或网络登录等方式,将Internet上大量网站的页面信息收集到本地,经过加工处理建立信息数据库和索引数据库。
社区管理员
  • 搜索引擎技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧