var vv='';
$("#contents").html(vv);
2.编写cgi程序,以cgictest.c为模板,修改 file()函数,从cgic存的临时文件(cgic将收到的上传文件存为名称cgicxxxxxx的临时文件,放在/tmp下)读出数据,并写入新的文件,
void File()
{
cgiFilePtr file;
int fd;
mode_t mode;
char name[1024];
char contentType[1024];
char buffer[1024];
int size;
int got;
if (cgiFormFileName("upfile", name, sizeof(name)) != cgiFormSuccess) {
printf("No file was uploaded.
"
);
return;
}
fprintf(cgiOut, "The filename submitted was: ");
cgiHtmlEscape(name);
fprintf(cgiOut, "
"
);
cgiFormFileSize("upfile", &size);
fprintf(cgiOut, "The file size was: %d bytes
"
, size);
cgiFormFileContentType("upfile", contentType, sizeof(contentType));
fprintf(cgiOut, "The alleged content type of the file was: ");
cgiHtmlEscape(contentType);
fprintf(cgiOut, "
"
);
fprintf(cgiOut, "Of course, this is only the claim the browser made when uploading the file. Much like the filename, it cannot be trusted.
"
);
fprintf(cgiOut, "The file's contents are shown here:
"
);
if (cgiFormFileOpen("upfile", &file) != cgiFormSuccess) {
fprintf(cgiOut, "Could not open the file.
"
);
return;
}
//打开或创建文件在当前目录,即/var/www/cgi-bin
//注意文件存取权限,否如会出现文件无法访问
mode=S_IRWXU|S_IRGRP|S_IROTH; //
fd = open(name,O_RDWR|O_CREAT|O_TRUNC,mode);
if(fd < 0){
fprintf(cgiOut, "Could not create the file,error:%d
"
,fd);
}
fprintf(cgiOut, "
"
);
while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==
cgiFormSuccess)
{
//写入文件
if(got>0){
write(fd,buffer,got);
}
cgiHtmlEscapeData(buffer, got);
}
fprintf(cgiOut, "
");
cgiFormFileClose(file);
close(fd);
}
3.编译cgi程序,得到cgiproc.cgi,放入 cgi-bin文件夹下,进行文件上传操作,中间出现如下几个问题
3.1 cgic库会将上传的文件在/tmp下存储临时文件 cgicxxxxxx,如图