Blog作者的回复:
int download_file( int type )
{
cgiFilePtr file;
char name[1024];
char *pFileName;
char filename[128];
char *pStr;
char contentType[1024];
char buffer[1024];
int size;
int got;
FILE *fp;
if(cgiFormFileName(pDownloadFile[type], name, sizeof(name)) != cgiFormSuccess)
{
printf("<p>No file was downloaded.<p>\n");
flag_success[type]=-1;
return -1;
}
pFileName = name;
while(1)
{
if((pStr = strstr(pFileName,"\\"))==NULL)
{
break;
}
pFileName = pStr + 1;
}
//fprintf(cgiOut, "The filename submitted was: ");
//cgiHtmlEscape(pFileName);
//fprintf(cgiOut, "<p>\n");
cgiFormFileSize(pDownloadFile[type], &size);
//fprintf(cgiOut, "The file size was: %d bytes<p>\n", size);
cgiFormFileContentType(pDownloadFile[type], contentType, sizeof(contentType));
//fprintf(cgiOut, "The alleged content type of the file was: ");
//cgiHtmlEscape(contentType);
//fprintf(cgiOut, "<p>\n");
//fprintf(cgiOut, "Of course, this is only the claim the browser made when downloading the file. Much like the filename, it cannot be trusted.<p>\n");
//fprintf(cgiOut, "The file's contents are shown here:<p>\n");
if (cgiFormFileOpen(pDownloadFile[type], &file) != cgiFormSuccess)
{
fprintf(cgiOut, "Could not open the file.<p>\n");
flag_success[type]=-1;
return -1;
}
//fprintf(cgiOut, "<pre>\n");
sprintf(filename,"%s%s",pTargetFile[type],pFileName);
if((fp = fopen(filename,"w+"))<0)
{
fprintf(cgiOut,"CGI无法打开该目标文件<BR>\n");
}
while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) == cgiFormSuccess)
{
// cgiHtmlEscapeData(buffer, got);
fwrite(buffer,sizeof(char),got,fp);
}
fclose(fp);
//fprintf(cgiOut, "</pre>\n");
cgiFormFileClose(file);
flag_success[type]=1;
return 0;
}
|