CGI 檔案下載
來源:互聯網
上載者:User
檔案名稱: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> http://hezhao2000.blog.163.com/blog/static/122436367201331742413578/