当前位置: 代码迷 >> CGI >> 这样写cgi文件上载如何不行呀,死小弟我了
  详细解决方案

这样写cgi文件上载如何不行呀,死小弟我了

热度:515   发布时间:2013-02-26 00:00:00.0
这样写cgi文件下载怎么不行呀,急死我了
Html代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis">
<title>下载</title>
</head>
 <body leftmargin="50">
  下载<p>
  <a href="download_proc1.cgi">文件下载<br></a><br>
</body>
</html>


服务器端(C语言):

int set_HTTP_CsvDownload(char *buf)
{
int j=0,sz=0;
 
memset(buf,0,1024);
 
sprintf(&buf[j],"%s","Content-Disposition: attachment;filename=myfile.txt\n");
j = strlen(buf);


sprintf(&buf[j],"%s","Content-Transfer-Encoding: binary\n");
j = strlen(buf);


sprintf(&buf[j],"%s","Accept-Ranges: bytes\n");
j = strlen(buf);


sprintf(&buf[j],"%s","Content-Length: 1024\n");
j = strlen(buf);


sprintf(&buf[j],"%s","Connection: close\n");
j = strlen(buf);


sprintf(&buf[j],"%s","Content-type: application/octet-stream\n");
j = strlen(buf);


        //读入文件
if((fp = fopen((const char *)("myfile.txt"), (char *)"r")) != NULL)
{
byte temp_buf[1024];
sz = fread(temp_buf, 1, 1024, fp);
fclose(fp);

memcpy(&buf[j],temp_buf,sz);
j+=sz;
}

return j;
}
服务器端将上面的buf发送到客户端浏览器上。但是客户端并不会出现文件下载框,而是在浏览器上显示出我读出来的txt文本本件里的内容。真不知道该怎么办了,急死我了。求高手指点!多谢!!

我是新人,没有积分,大家帮帮忙,谢谢。
------解决方案--------------------------------------------------------
sprintf(&buf[j],"%s","Content-type: application/octet-stream\n");
这一行改成两个 \n, 即:
sprintf(&buf[j],"%s","Content-type: application/octet-stream\n\n");
  相关解决方案