MFC如何讲图片上传到 http://169.254.1.10:8090/api/v1/~bali/ABC0123456789
时间: 2023-10-05 07:07:10 浏览: 145
aurora-engine/aurora
MFC可以使用WinINet库来实现图片上传功能。以下是一个示例代码:
```cpp
#include <afxinet.h>
BOOL UploadImage(LPCTSTR imagePath)
{
HINTERNET hInternet = InternetOpen(_T("MFC App"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hInternet == NULL)
{
return FALSE;
}
HINTERNET hConnect = InternetConnect(hInternet, _T("169.254.1.10"), 8090, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (hConnect == NULL)
{
InternetCloseHandle(hInternet);
return FALSE;
}
HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"), _T("/api/v1/~bali/ABC0123456789"), NULL, NULL, NULL, 0, 0);
if (hRequest == NULL)
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return FALSE;
}
CString fileName = PathFindFileName(imagePath);
CString contentType = _T("image/jpeg");
CString boundary = _T("boundary1234567890");
CString header;
header.Format(_T("Content-Type: multipart/form-data; boundary=%s"), boundary);
HttpAddRequestHeaders(hRequest, header, -1, HTTP_ADDREQ_FLAG_ADD);
CString body;
body += _T("--") + boundary + _T("\r\n");
body += _T("Content-Disposition: form-data; name=\"file\"; filename=\"") + fileName + _T("\"\r\n");
body += _T("Content-Type: ") + contentType + _T("\r\n\r\n");
CString footer;
footer.Format(_T("\r\n--%s--\r\n"), boundary);
DWORD fileSize = GetFileSize(imagePath);
DWORD totalSize = body.GetLength() + fileSize + footer.GetLength();
CString contentLength;
contentLength.Format(_T("Content-Length: %u"), totalSize);
HttpAddRequestHeaders(hRequest, contentLength, -1, HTTP_ADDREQ_FLAG_ADD);
BOOL result = HttpSendRequest(hRequest, NULL, 0, (LPVOID)(LPCTSTR)body, body.GetLength());
if (result)
{
HANDLE hFile = CreateFile(imagePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
BYTE buffer[1024];
DWORD bytesRead = 0;
DWORD bytesWritten = 0;
while (ReadFile(hFile, buffer, sizeof(buffer), &bytesRead, NULL) && bytesRead > 0)
{
result = InternetWriteFile(hRequest, buffer, bytesRead, &bytesWritten);
if (!result)
{
break;
}
}
CloseHandle(hFile);
}
else
{
result = FALSE;
}
if (result)
{
result = InternetWriteFile(hRequest, footer, footer.GetLength(), NULL);
}
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return result;
}
DWORD GetFileSize(LPCTSTR filePath)
{
DWORD fileSize = 0;
HANDLE hFile = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
fileSize = GetFileSize(hFile, NULL);
CloseHandle(hFile);
}
return fileSize;
}
```
在上面的代码中,`UploadImage`函数接受一个图片文件的路径作为参数,然后使用WinINet库创建一个HTTP连接,并发送POST请求来上传图片。其中,`GetFileSize`函数用来获取文件大小,`boundary`用来分隔多个HTTP部分,`body`和`footer`分别表示HTTP请求的头部和尾部,`contentLength`表示HTTP请求的正文长度。注意,这个示例代码中使用的是multipart/form-data格式上传文件,因此需要在HTTP头部中指定Content-Type为multipart/form-data,并在正文中使用boundary来分隔每个部分。在本例中,包含两个部分,一个是文件的元数据,另一个是文件内容。最后,发送HTTP请求并写入文件内容即可上传文件。
阅读全文