【发布时间】:2012-04-22 20:47:41
【问题描述】:
我正在开发一个必须与浏览器配合使用的简单服务器。当我给它一些命令时,它必须用一些 html 代码回复我,以便回复我的答案。例如,我可以看到文件夹等文件的列表。我可以使用 localhost:port/somecommand 访问我的服务器 现在我正在从本地硬盘下载文件。我想要做的是输入一个像 localhost:port/download/filepath 这样的 url 并让浏览器下载它。当我创建回复时,我把所有东西 html 都需要了解有一个文件要下载,事实上我有一个经典的弹出窗口,要求我下载文件,但我收到的比原来的文件大到硬盘,实际上文件已损坏。 代码如下:
这是我发回的html
HTTP/1.0 200 OK
Date: Tue Apr 10 16:23:55 2012
Content-Type: application/octet-stream
Content-Disposition: attachment; filename=mypic.jpg
Content-Length: 2574359
//empty line here
(我关注了 The Content-Disposition Header Field text)
然后我从文件中读取,然后我先发回 html,然后从文件中读取:
int file=open(name,O_RDONLY);
void * data=malloc(buf.st_size+1); //buf.st_size is the correct file size
size_t readed=read(file,data,buf.st_size);
send(sd_current, html, DIRSIZE, 0); //html is a string containing what you I showed you
send(sd_current, data, readed);
这导致我可以使用 localhost:port/download/filepath 下载的文件比原始文件大然后损坏,但我无法摆脱原因。有人可以帮我吗?
【问题讨论】:
-
send(sd_current, html,DIRSIZE);您打算将其设为 DIRSIZE 还是 html 字符串的长度? -
是的,尝试从一个版本到另一个我忘了改变这个..我现在改为 strlen(html),现在我下载的文件只大了 2 个字节!我们很接近:D
-
好的解决了!!谢谢大家!就是这该死的 DIRSIZE :D 谢谢!
标签: c http attachment send