MFC 上传图片到http://169.254.1.10:8090/api/v1/~bali/ABC0123456789
时间: 2023-10-04 20:06:47 浏览: 56
您可以使用 WinINet 库来实现 MFC 上传图片到指定 URL 的功能。以下是一个简单的示例代码:
```cpp
BOOL CMyApp::UploadImage(LPCTSTR lpszImagePath)
{
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
// 打开一个会话
hSession = InternetOpen(_T("MyApp"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession == NULL)
{
return FALSE;
}
// 连接到指定的 URL
hConnect = InternetConnect(hSession, _T("169.254.1.10"), 8090, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (hConnect == NULL)
{
InternetCloseHandle(hSession);
return FALSE;
}
// 构造 HTTP 请求
hRequest = HttpOpenRequest(hConnect, _T("POST"), _T("/api/v1/~bali/ABC0123456789"), NULL, NULL, NULL, 0, 0);
if (hRequest == NULL)
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return FALSE;
}
// 打开文件
CFile file;
if (!file.Open(lpszImagePath, CFile::modeRead | CFile::shareDenyWrite))
{
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return FALSE;
}
// 获取文件大小
DWORD dwFileSize = (DWORD)file.GetLength();
// 发送 HTTP 请求头
CString strHeaders = _T("Content-Type: image/jpeg\r\nContent-Length: ") + CString(dwFileSize) + _T("\r\n");
if (!HttpSendRequest(hRequest, strHeaders, strHeaders.GetLength(), NULL, 0))
{
file.Close();
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return FALSE;
}
// 发送文件内容
const DWORD BUFFER_SIZE = 4096;
BYTE buffer[BUFFER_SIZE];
DWORD dwBytesRead = 0;
DWORD dwBytesWritten = 0;
while (dwBytesRead < dwFileSize)
{
DWORD dwBytesToRead = min(dwFileSize - dwBytesRead, BUFFER_SIZE);
if (!file.Read(buffer, dwBytesToRead))
{
file.Close();
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return FALSE;
}
if (!InternetWriteFile(hRequest, buffer, dwBytesToRead, &dwBytesWritten))
{
file.Close();
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return FALSE;
}
dwBytesRead += dwBytesToRead;
}
file.Close();
// 确认 HTTP 请求已完成
if (!InternetCloseHandle(hRequest))
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return FALSE;
}
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return TRUE;
}
```
您只需要将 `lpszImagePath` 参数替换成要上传的图片文件路径即可。需要注意的是,该示例代码假定上传的是 JPEG 格式的图片,如果上传的是其他格式的图片,需要相应地修改 Content-Type 头部。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)