文件上传进度条 c 语言,cgi 上传文件(c 语言) 进度条显示
//虚拟机上搭建apache服务器上传文件
//1.代码:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "dirent.h"
#include
#include
static int atoii (char *zzzz)
{
int i = 0;
int num=0;
for(i=0;i<20;i++)
{
if(zzzz[i] >= '0'
&& zzzz[i] <= '9')
{
num =
num * 10 + (zzzz[i] - '0');
}else
{
break;
}
}
return num;
}
char* getCgiData(FILE* fp, char* requestmethod)
{
char* input;
int len;
char *pppp;
int size = 1024;
int i = 0;
if(!strcmp(requestmethod, "GET"))
{
input = getenv("QUERY_STRING");
return input;
}
else if (!strcmp(requestmethod, "POST"))
{
pppp=getenv("CONTENT_LENGTH");
len = atoii(pppp);
input = (char*)malloc(sizeof(char)*(size + 1));
if (len == 0)
{
input[0] = '\0';
return input;
}
fgets(input, len+1, stdin);
input[len]='\0';
return input;
}
return NULL;
}
static unsigned int tmppp=0;
char *getFileName(unsigned char *req)
{
int i;
int leng;
tmppp=0;
char *psz1; char *psz2;
unsigned char
*cur_post,*buf;
// get filename keyword
if ((psz1=strstr(req,
"filename=")) == NULL)
{
return (char
*)&tmppp;
}
// get pointer to actual
filename (it's in quotes)
psz1+=strlen("filename=");
if ((psz1 = strtok(psz1,
"\"")) == NULL)
{
return (char
*)&tmppp;
}
// remove leading path
for both PC and UNIX systems
if ((psz2 =
strrchr(psz1,'\\')) != NULL)
{
psz1 = psz2+1;
}
if ((psz2 =
strrchr(psz1,'/')) != NULL)
{
psz1 = psz2+1;
}
return psz1;
}
main()
{
char *reqMethod;
char *wp;
char *var=NULL;
int len;
long total,i,count;
char
*fileName,*ps1,*ps2;
char *fileN;
char Boundary[256];
char errorBuf[200]="";
char tmpBuf[512];
char
filePath[256]="/usr/local/apache2/webdav/";//directory of uploaded
file
FILE *fileBuf=NULL;
reqMethod=getenv("REQUEST_METHOD");
len=atoii(getenv("CONTENT_LENGTH"));
printf("%s","Content-type:text/html \r\n\r\n");
//printf("
http-equiv=\"Content-Type\"
content=\"text/html;charset=UTF-8\">");
Boundary[0] = '\r';
Boundary[1] = '\n';
Boundary[2] = '\0';
if
(fgets(&Boundary[2], sizeof(Boundary)-2, stdin) ==
NULL)
{
sprintf(errorBuf,"%s","Get boundary failed !");
goto error;
}
//strip terminating CR /
LF
if
((ps1=strchr(&Boundary[2],'\r')) != NULL)
{
*ps1 = '\0';
}
if
((ps1=strchr(&Boundary[2],'\n')) != NULL)
{
*ps1 = '\0';
}
//printf("Boundry=\"%s\"
",Boundary);
//printf("content-length=%d
",len);
fgets(tmpBuf,512,stdin);
//printf("All=%s
",tmpBuf);
fileName=getFileName(tmpBuf);
if(fileName)
{
//printf("fileName=%s
",fileName);
}
strcat(filePath,fileName);
//printf("filepath===%s
",filePath);
memset(tmpBuf,512,0x00);
fgets(tmpBuf,512,stdin);
//printf("%s
",tmpBuf);//content-type
memset(tmpBuf,512,0x00);
fgets(tmpBuf,512,stdin);
//printf("%s
",tmpBuf);// \r\n
if(fopen(filePath,"rb"))
{
sprintf(errorBuf,"%s","File already exist.");
goto error;
}
if ((fileBuf =
fopen(filePath, "wb+")) == NULL)
{
sprintf(errorBuf,"%s","File open error.Make sure
you have the permission.");
goto error;
}
// copy the file
while
((count=fread(tmpBuf, 1, 512, stdin)) != 0)
{
if ((fwrite(tmpBuf, 1, count, fileBuf)) !=
count)
{
sprintf(errorBuf,"%s","Write file error.");
goto
error;
}
}
// re read last 128
bytes of file, handling files < 128 bytes
if ((count =
ftell(fileBuf)) == -1)
{
goto error;
}
if (count
> 128)
{
count = 128;
}
if (fseek(fileBuf,
0-count, SEEK_END) != 0)
{
goto error;
}
// get the new position
if ((total =
ftell(fileBuf)) == -1)
{
goto error;
}
// and read the data
from fileBuf
count = fread(tmpBuf, 1,
sizeof(tmpBuf), fileBuf);
tmpBuf[count] = '\0';
//printf("count=%ld
",count);
// determine offset of
terminating boundary line
for (i=0;
i<=(count); i++)//-(long)strlen(Boundary)
{
//printf("%c",tmpBuf[i]);
if (tmpBuf[i] == Boundary[0])
{
//printf("found /r
");
if(strncmp(Boundary, &tmpBuf[i], strlen(Boundary))
== 0)
{
total+=i;
// printf("find
boudary.
");
break;
}
}
}
//printf("
i=%ld
",i);
//printf("total=%ld
",total);
if (fseek(fileBuf,total,
SEEK_SET) != 0)
{
goto error;
}
if ((total =
ftell(fileBuf)) == -1)
{
goto error;
}
//printf("total=%ld
",total);
// truncate the
terminating boundary line .
int fd=fileno(fileBuf);
ftruncate(fd,total);
fflush(fileBuf);
error:
if (fileBuf != NULL)
{
fclose(fileBuf);
}
if(errorBuf[0]!='\0')
//打印信息到网页的隐藏的iframe中
printf("",errorBuf);
else
{//printf("file upload
success !
");
printf("
success!');parent.location.replace('upload.html');");
}
return;
}
2. 编译代码
gcc -o upload upload.c
3.网页文件,因无法通过ajax实现无刷新上传文件,只好借用隐藏的
iframe实现。包含有进度条哦!
[html] view plaincopy
?
content="text/html; charset=UTF-8">
content=no-cache>
CONTENT="NO-CACHE">
var time=0;
var delay_time=1000;
var loop_num=0;
function getRefToDivNest(divID, oDoc)
{
if( !oDoc ) { oDoc = document; }
if( document.layers ) {
if( oDoc.layers[divID] )
{ return oDoc.layers[divID]; } else {
for( var x = 0, y; !y
&& x <
oDoc.layers.length; x++ ) {
y =
getRefToDivNest(divID,oDoc.layers[x].document); }
return y; } }
if( document.getElementByIdx ) { return
document.getElementByIdx(divID); }
if( document.all ) { return
document.all[divID]; }
return document[divID];
}
function progressBar( oBt, oBc, oBg, oBa, oWi, oHi, oDr )
{
MWJ_progBar++; this.id = 'MWJ_progBar' +
MWJ_progBar; this.dir = oDr; this.width = oWi; this.height = oHi;
this.amt = 0;
//write the bar as a layer in an ilayer in
two tables giving the border
document.write( '
"progress_div" class = "off" >
border="0" cellspacing="0" cellpadding="'+oBt+'">'+
'
Please
wait...
bgcolor="'+oBc+'">'+
'
cellspacing="0"
cellpadding="0">
height="'+oHi+'" width="'+oWi+'" bgcolor="'+oBg+'">'
);
if( document.layers ) {
document.write(
'
width="'+oWi+'">
bgcolor="'+oBa+'"
name="MWJ_progBar'+MWJ_progBar+'">
'
);
} else {
document.write(
'
style="position:relative;top:0px;left:0px;height:'+oHi+'px;width:'+oWi+';">'+
'
style="position:absolute;top:0px;left:0px;height:0px;width:0;font-size:1px;background-color:'+oBa+';"
id="MWJ_progBar'+MWJ_progBar+'">
'
);
}
document.write(
'
\n'
);
this.setBar = resetBar; //doing this inline
causes unexpected bugs in early NS4
this.setCol = setColour;
}
function resetBar( a, b )
{
//work out the required size and use
various methods to enforce it
this.amt = ( typeof( b ) == 'undefined' ) ?
a : b ? ( this.amt + a ) : ( this.amt - a );
if( isNaN( this.amt ) ) { this.amt = 0; }
if( this.amt > 1 ) { this.amt = 1; } if( this.amt
< 0 ) { this.amt = 0; }
var theWidth = Math.round( this.width * ( (
this.dir % 2 ) ? this.amt : 1 ) );
var theHeight = Math.round( this.height * (
( this.dir % 2 ) ? 1 : this.amt ) );
var theDiv = getRefToDivNest( this.id );
if( !theDiv ) { window.status = 'Progress: ' + Math.round( 100 *
this.amt ) + '%'; return; }
if( theDiv.style ) { theDiv = theDiv.style;
theDiv.clip = 'rect(0px '+theWidth+'px '+theHeight+'px 0px)'; }
var oPix = document.childNodes ? 'px' : 0;
theDiv.width = theWidth + oPix;
theDiv.pixelWidth = theWidth; theDiv.height = theHeight + oPix;
theDiv.pixelHeight = theHeight;
if( theDiv.resizeTo ) { theDiv.resizeTo(
theWidth, theHeight ); }
theDiv.left = ( ( this.dir != 3 ) ? 0 :
this.width - theWidth ) + oPix; theDiv.top = ( ( this.dir != 4 ) ?
0 : this.height - theHeight ) + oPix;
}
function setColour( a )
{
//change all the different colour styles
var theDiv = getRefToDivNest( this.id );
if( theDiv.style ) { theDiv = theDiv.style; }
theDiv.bgColor = a; theDiv.backgroundColor
= a; theDiv.background = a;
}
function show_div(show,id) {
if(show){
document.getElementByIdx(id).style.display =
"block";
}else{
document.getElementByIdx(id).style.display =
"none";
}}
function progress()
{if (time < 1){time = time + 0.033;}
else {time = 0;
loop_num++;
}
setTimeout('progress()',delay_time);
myProgBar.setBar(time);
}
function sendClicked(F)
{ if(document.usb_upload.uploadedfile.value == ""){
document.usb_upload.uploadedfile.focus();
alert('File name can not be empty !');
return false;
}
F.submit();
show_div(true, "progress_div");
progress();}
function init(){show_div(0, "progress_div");}
upload
file
enctype="multipart/form-data" action="/cgi-bin/upload"
method="post"
target="hidden_frame">
Select File:
type="file" size="35" name="uploadedfile">
οnclick=sendClicked(this.form) type=button name="Upload"
value="Upload">
style='display:none'>
style='display:none'>
var MWJ_progBar = 0;
var myProgBar = new progressBar(1,'#2e2e2e',
'#2e2e2e','#043db2',300,15,1);
id="current_directory"
style="display:none">
4.效果不错,上传几百兆文件无压力。
转载请注明出处http://blog.csdn.net/fengchao_2009/article/details/7699292
文件上传进度条 c 语言,cgi 上传文件(c 语言) 进度条显示相关推荐
- php flash上传进度条,PHP_PHP+FLASH实现上传文件进度条相关文件 下载,PHP之所以很难实现上传进度条 - phpStudy...
PHP+FLASH实现上传文件进度条相关文件 下载 PHP之所以很难实现上传进度条是因为在我们上传文件到服务器的时候,要等到文件全部送到服务器之后,才执行相应的php文件.在这之前,文件数据保存在一个 ...
- C语言cgi解析上传文件的本地测试
首先用浏览器提交提交一个multipart/form-data类型的数据包到本地服务器,随便用什么脚本写个接受数据包的程序,保存数据包到文件,数据包的格式大概就是这样: ------WebKitFor ...
- cp linux 显示进度条_Unix/Linux/Mac os下 文件互传
Unix/Linux/Mac os下 文件互传 说起文件互传,就不得不提命令scp. 他是Secure copy的缩写,使用ssh连接和加密方式, 如果两台机器之间配置了ssh免密登录, 那在使用sc ...
- boa+cgi上传文件
boa+cgi上传文件 从网上下的开源代码boa+cgi,移植到arm里做个在线升级,曾以为最难的flash擦写先搞定了,却卡在上传文件这块.问题是超过400K的文件不能上传,连.cgi文件也没有执行 ...
- java jsch实现sftp文件上传,并且控制上传的速度,同时监控上传进度
工作中,有些环境带宽有限,比如说专线,通常带宽比较小,又不便宜,当业务量大的时间,如果在专线上还要传输文件的话,往往在文件传输的时间会导致带宽占慢,就有可能导致时实交易进不来,有可能影响交易,今天贴一 ...
- Spring MVC-09循序渐进之文件上传(基于Servlet3.0+Html5客户端上传文件)
示例 测试 源码 虽然Servlet3.0+中上传文件,我们在服务端编程即可非常容易,但是用户体验却不是非常友好.单独的一个HTML表单并不能显示进度条,或者显示已经成功上传的文件数量. 不管是Jav ...
- 大文件分片上传前端框架_无插件实现大文件分片上传,断点续传
文件上传.gif 1. 简介: 本篇文章基于实际项目的开发,将介绍项目中关于大文件分片上传.文件验证.断点续传.手动重试上传等需求的使用场景及实现: 2. 项目需求 在一个音视频的添加中,既要有音视频 ...
- 不支持所上传的文件格式。请确认选择的文件无误。_职场人必备!如何把PDF文件转换成word文档?...
点击箭头处"蓝色字",关注我们哦!! 再撑一下!明天就是周末了! PDF大家都用过,它是平时我们办公中最常用到的文件格式之一.它是一种可移植文档格式,与操作系统平台无关,在任何系统 ...
- pdf压缩文件怎么压缩最小,pdf大小超过上传大小不能上传怎么压缩?
pdf压缩文件怎么压缩最小?pdf是日常办公中比较常见的一种文件格式,但是有时候pdf文件的容量会比较大,上传到某些平台会有限制,那么我们就需要压缩pdf文件,但是有时候压缩处理会模糊,今天我给大家介 ...
- 前端上传预览文件以及下载,node后端存储文件以及返回前端文件流下载
上传文件<div class="inputBox" style="background-color: rgb(63, 137, 212);">上传文 ...
最新文章
- Windows客户端C/C++编程规范“建议”——函数调用
- Android recipe 在代码中写布局
- 先验概率,后验概率,最大似然估计,最大后验概率
- mysql php查询错误_php查询mysql数据库错误
- 福建物联网产业发展势头良好
- 如何破解Synplify Pro 9.6.2? (SOC) (Synplify)
- logback-spring.xml配置文件
- OC中的基本容器和基本数据类型
- Python 数据分析三剑客之 NumPy(一):理解 NumPy / 数组基础
- 7-2 城市间紧急救援 (25 分)
- 一张纸厚度是多少毫米_大洼20毫米水泥纤维板多少钱一张追求至善
- 数据分析中的可视化-常见图形
- 诗与远方:无题(十二)
- Scapy 中文文档:一、介绍
- usleep头文件_Linunx的sleep,usleep,select,nonasleep对比与应用
- matlab绘制二元一次函数图像_基于MATLAB的数学图像绘制
- 编译原理 --- 递归下降分析器
- 计算机毕业设计SSM电影票网上订票系统【附源码数据库】
- GBase 8s 常用管理员命令汇总-onstat(下)
- zh-cn 和zh的意思
热门文章
- php 分享微博,PHP调用微博接口实现微博登录
- Ngxin虚拟主机的三种配置方法
- 720yun全景图+vue
- 结巴分词--关键词抽取
- python 爬虫学习之 selenium.webdriver学习
- python读取xlsm文件_如何从* .xlsm中提取工作表并将其保存为Python中的* .csv?
- SpringCloud版本Hoxton SR5 --- 第二讲:eureka 、eureka与zookeeper的区别与使用场景。
- lol人物模型提取(八)
- Redhat认证体系
- 机器学习之logistic 回归