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/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.