c++로 웹에서 파일전송을 하려고 했는데 생각대로 잘 안됩니다.

guu의 이미지

안녕하세요.

php, 파이썬을 조금하다가 오늘 c++를 사용했습니다. 웹에서 파일 다운로드를 시키는걸 c++로 만들려고 하는데 생각대로 잘 안되네요.

#include <iostream.h>
#include <fstream.h>
#include <string.h>
 
int main () {
	std::string line;
	ifstream file("C:/file.mp3");
	if (file.is_open()) {
		int i = 0;
		long begin, end, size;
 
		begin = file.tellg();
		file.seekg (0, ios::end);
		end = file.tellg();
		size = end-begin;
		cout << "Content-Type: audio/mp3\n\n";
		cout << "Content-Length: " << size << "\n";
		cout << "Content-Disposition: attachment; filename=file.mp3\n";
		cout << "Cache-Control: no-store, no-cache, must-revalidate\n";
		cout << "Pragma: no-cache\n";
		cout << "Expires: -1\n";
 
		while(!file.eof()) {
			i++;
			if (i != 1) {
				cout << "\n";
			}
			getline(file, line);
			cout << line;
		}
		file.close();
	}
	else {
		cout << "Content-Type: text/html; charset=utf-8\n\n";
		cout << "Unable to open file";
	}
	return 0;
}

아직 아는게 없어서 제대로 된건지도 모르겠습니다. 컴파일하고 웹에서 실행시키면 다운로드는 됩니다. 파일형식은 mp3형식이라고 나와있는데 다운을 받아보면 .cgi 파일 그대로 다운을 받고 내용을보면

Content-Length: 9369685
Content-Disposition: attachment; filename=file.mp3
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Expires: -1

이렇게만 되있습니다. 아는게 없어서 왜그런지 도저히 모르겠습니다. 도와주세요. ( _ _ )

정태영의 이미지

cout << "Content-Type: audio/mp3\n\n";

뉴라인을 한번만 쓰세요.

--
오랫동안 꿈을 그리는 사람은 그 꿈을 닮아간다...

http://mytears.org ~(~_~)~
나 한줄기 바람처럼..

오랫동안 꿈을 그리는 사람은 그 꿈을 닮아간다...

http://mytears.org ~(~_~)~
나 한줄기 바람처럼..

guu의 이미지

뉴라인을 한번만 쓰면 500 에러가 떠서요. 에러문은 이렇습니다.

[Sun May 25 20:33:12 2008] [error] [client 127.0.0.1] Premature end of script headers: music.cgi

guu의 이미지

계속 하다보니 헤더는 제대로 줬는데 파일 읽는걸 어떻게 해야할지 모르겠네요. 제가 볼땐 이상이 없는데 출력이 안되네요. 파일 읽어서 뿌려주는것좀 도와주세요.

guu의 이미지

파일을 아예 출력을 하지 못했었는데, 계속 하다보니 파일을 출력은 할수 있게됬습니다. 그런데 마지막 몇 라인이 출력이 되지 않네요. 그래서 파일을 재생시켜 보면 이상한 소리만 나옵니다. 그리고 텍스트 파일도아니고 꼭 라인을 구할 필요도 없는데 저렇게 라인마다 출력을 하게 해야하나요? 그냥 파이썬처럼 file.read() 이렇게는 사용하지 못하나요?

궁금한건 많은데 알길이 없어서 답답합니다. 귀찮더라도 아시는 분 도움좀 주세요. ( _ _ )

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.