gpt4 book ai didi

c++ - C++ 如何将文件发送到浏览器

转载 作者:行者123 更新时间:2023-11-28 01:09:34 25 4
gpt4 key购买 nike

我需要将文件从我的目录发送给用户。问题文件未发送。谁能帮帮我?

我的代码是这样的:

CHttpServerContext* pCtxt;
// ... there i set headers for open

DWORD dwRead;
CString fileName = "c:\txt.doc";

HANDLE hFile = CreateFile (fileName, GENERIC_READ, FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, (HANDLE) NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return;
}


int c = 0;
CHAR szBuffer [2048];
do
{
if (c++ > 20) {
break;
return;
}
// read chunk of the file
if (!ReadFile (hFile, szBuffer, 2048, &dwRead, NULL))
{
return;
}
if (!dwRead)
// EOF reached, bail out
break;

// Send binary chunk to the browser
if (!pCtxt->WriteClient( szBuffer, &dwRead, 0))
{
return;
}
}
while (1);
CloseHandle (hFile);
}

最佳答案

医生,我病了。我怎么了?

我的意思是,您几乎没有提供有关发生的事情的任何信息。

  1. 您知道您的代码中是否有某些函数向您返回错误吗?
  2. 为什么在 20 次迭代后中止循环?这将您限制为 40KB。
  3. 您究竟如何初始化 CHttpServerContext
  4. 如果您只是按原样发送文件,您可能会使用高性能的 TransmitFile 函数。
  5. 你的客户是什么?你怎么知道它没有得到文件?

关于c++ - C++ 如何将文件发送到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4123830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com