我写了一个 IIS 模块,它工作正常。我想从请求中获取发布的文件。
在下面的代码中,我可以获得发送的表单数据并在标题中查看文件:
IHttpRequest * pHttpRequest = pHttpContext->GetRequest();
if (pHttpRequest != NULL)
{
LPCSTR body;
DWORD cbBytesReceived = 1024;
void * pvRequestBody = pHttpContext->AllocateRequestMemory(cbBytesReceived);
if (NULL == pvRequestBody)
{
pProvider->SetErrorStatus(hr);
return RQ_NOTIFICATION_FINISH_REQUEST;
}
if (pHttpRequest->GetRemainingEntityBytes() > 0)
{
while (pHttpRequest->GetRemainingEntityBytes() != 0)
{
hr = pHttpRequest->ReadEntityBody(
pvRequestBody, cbBytesReceived, false, &cbBytesReceived, NULL);
if (FAILED(hr))
{
if (ERROR_HANDLE_EOF != (hr & 0x0000FFFF))
{
pProvider->SetErrorStatus(hr);
return RQ_NOTIFICATION_FINISH_REQUEST;
}
}
body = (LPCSTR)pvRequestBody;
pszResult = pszResult + body;
}
}
}
结果是:
-----------------------5r707ac9a9687yu
Content-Disposition: form-data; name="file"; filename="1.zip"
Content-Type: application/octet-stream
PKþóÐMš`_ÿív(X[úM/xvœ5a¢~¯²²ÊÆÎÈá"}å
lIœì*7·®-W§Xþn¹DçvÃŒØÀ>ÊHñ\N-kÂ¥ûºÂm'ŒäõÚÌŸÏŽ)ããSqŽT3ÕïDñ?ËWÇKy«zAÉ÷øŒ¿ÂÇ
我试图将文件部分(PKþóÐMš`_ÿív ...)转换为二进制文件并写入文件,但文件被curropted,因为标题包含所有信息(所有发布的文件,内容存储,...)而不是清晰的二进制文件。
如何将发送的文件写入磁盘(二进制/文本)?