CGI 文件下载

80 篇文章 0 订阅
42 篇文章 0 订阅
文件名:download.c 用交叉编译工具编译生成download
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/stat.h>
#include<unistd.h>

#define MAX_FILE_LEN  (1024*30)
#define DOWNLOAD_FILE_PATH "/works/config/"
#define DOWNLOAD_FILE_NAME "SWConf.ini"

int main(){
FILE *fp;
char filebuf[MAX_FILE_LEN];
char cmd[512];
struct stat sb;
sprintf(cmd, "%s%s", DOWNLOAD_FILE_PATH, DOWNLOAD_FILE_NAME);
stat(cmd, &sb); //取待下载文件的大小
//输出HTTP头信息,输出附加下载文件、文件长度以及内容类型
printf("Content-Disposition:attachment;filename=%s", DOWNLOAD_FILE_NAME);
printf("\r\n"); 
printf("Content-Length:%d", sb.st_size);
printf("\r\n");
// printf("Content-Type:application/octet-stream %c%c", 13,10); (ascii:"13--\r", "10--\n") 与下一行等同
printf("Content-Type:application/octet-stream\r\n");
printf("\r\n");
sprintf(cmd, "%s%s", DOWNLOAD_FILE_PATH, DOWNLOAD_FILE_NAME);
if(fp=fopen(cmd, "r+b")){  
//成功打开文件,读取文件内容
do{
int rs = fread(filebuf, 1, sizeof(filebuf), fp);
fwrite(filebuf, rs, 1, stdout);
}while(!feof(fp));
fclose(fp);
}

return 1;
}

html中代码的一部分是:超链接。
<tr>
<th>文件下载:</th>
<td> <a href="/cgi-bin/download">文件下载</a>
</td>
</tr>
html中代码的一部分是:button。
$('#swdownload').click(function(){
location.href="/cgi-bin/download";
});
<tr>
<th>文件下载:</th>
<td>
<input type="button" id="swdownload" value="下载">
</td>
</tr>
 
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值