在MFC环境中,可以使用WinINet库来实现HTTPS的文件上传功能。下面是一个示例代码,实现了向服务器上传文件的功能,同时传递了4个参数:path、file、md5_code和二进制文件内容。

#include <afxinet.h>

BOOL UploadFileToServer(const CString& serverURL, const CString& filePath, const CString& fileName, const CString& md5Code, const CString& fileContent)
{
    HINTERNET hInternet = InternetOpen(L"FileUpload", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if (hInternet == NULL)
    {
        AfxMessageBox(L"InternetOpen failed!");
        return FALSE;
    }

    HINTERNET hConnect = InternetOpenUrl(hInternet, serverURL, NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_SECURE, 0);
    if (hConnect == NULL)
    {
        InternetCloseHandle(hInternet);
        AfxMessageBox(L"InternetOpenUrl failed!");
        return FALSE;
    }

    CString boundary = L"---------------------------1234567890";
    CString contentType = L"multipart/form-data; boundary=" + boundary;

    CString header = L"Content-Type: " + contentType + L"\r\n";
    CString data;
    data += L"--" + boundary + L"\r\n";
    data += L"Content-Disposition: form-data; name=\"path\"\r\n\r\n";
    data += filePath + L"\r\n";
    data += L"--" + boundary + L"\r\n";
    data += L"Content-Disposition: form-data; name=\"file\"\r\n\r\n";
    data += fileName + L"\r\n";
    data += L"--" + boundary + L"\r\n";
    data += L"Content-Disposition: form-data; name=\"md5_code\"\r\n\r\n";
    data += md5Code + L"\r\n";
    data += L"--" + boundary + L"\r\n";
    data += L"Content-Disposition: form-data; name=\"file\"; filename=\"file.bin\"\r\n\r\n";
    data += fileContent + L"\r\n";
    data += L"--" + boundary + L"--\r\n";

    DWORD dataSize = data.GetLength() * sizeof(TCHAR);
    DWORD bytesWritten = 0;
    BOOL result = HttpSendRequest(hConnect, header, header.GetLength(), (LPVOID)(LPCTSTR)data, dataSize);
    if (!result)
    {
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hInternet);
        AfxMessageBox(L"HttpSendRequest failed!");
        return FALSE;
    }

    InternetCloseHandle(hConnect);
    InternetCloseHandle(hInternet);

    return TRUE;
}

使用示例:

CString serverURL = L"https://example.com/upload";
CString filePath = L"/server/path";
CString fileName = L"file.txt";
CString md5Code = L"md5_code_here";
CString fileContent = L"binary_file_content_here";

BOOL result = UploadFileToServer(serverURL, filePath, fileName, md5Code, fileContent);
if (result)
{
    AfxMessageBox(L"File uploaded successfully!");
}
else
{
    AfxMessageBox(L"File upload failed!");
}

请注意,示例代码中的serverURL、filePath、fileName、md5Code和fileContent变量需要根据实际情况进行替换。另外,如果需要进行HTTPS连接,需要在工程设置中添加Wininet.lib库的链接

在mfc环境实现一个https的文件上传要求from data参数为4个分别为path代表服务器路径、file代表文件名称、md5_code代表md5值、file代表二进制文件内容

原文地址: https://www.cveoy.top/t/topic/indX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录