Varun Varun - 1 month ago 22
C++ Question

File downloading without extension Socket Programming

I am working in socket programming with help of C++.And I have to write a code to download file.
My Http Header for this is :

char header[] = "HTTP/1.1 200 OK\r\nContent-Type:application/vnd.ms-excel;Content-Disposition:attachment;filename:\"abc.xls\";Content-Length:14; \r\n\r\n";


but file get downloaded as "download" it does not have extension too.
I also tried this with

char header[] = "HTTP/1.1 200 OK\r\nContent-Type:application/octet-stream;Content-Disposition:attachment;filename:\"abc.xls\";Content-Length:14; \r\n\r\n";


but its not working.
Can anyone help me regarding this.?

Answer

The HTTP response header you've tried looks like this:

HTTP/1.1 200 OK
Content-Type:application/vnd.ms-excel;Content-Disposition:attachment;filename:"abc.xls";Content-Length:14;

instead it should look like this

HTTP/1.1 200 OK
Content-Type:application/vnd.ms-excel
Content-Disposition:attachment;filename="abc.xls"
Content-Length:14

I recommend you study the relevant standards before trying to implement a protocol. In this case this is the HTTP standard (RFC 7230 and following RFC) and "Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)" (RFC 6266).

Comments