2 weixin 40054559 weixin_40054559 于 2017.09.01 01:16 提问

c++MFC实现图片文字传输

MFC做,本地读取图片文字发送到服务器端进行替换!post方法!跪求好心人给个完整代码,网上找的一堆错误!

1个回答

jkl012789
jkl012789   2017.09.01 09:34
3.文件上传
#include <afxinet.h>
int UploadFile(LPCTSTR strURL, //负责接收上传操作的页面的URL
    LPCTSTR strLocalFileName)  //待上传的本地文件路径
{
    ASSERT(strURL != NULL && strLocalFileName != NULL);

    BOOL bResult = FALSE;
    DWORD dwType = 0;
    CString strServer;
    CString strObject;
    INTERNET_PORT wPort = 0;
    DWORD dwFileLength = 0;
    char * pFileBuff = NULL;

    CHttpConnection * pHC = NULL;
    CHttpFile * pHF = NULL;
    CInternetSession cis;

    bResult = AfxParseURL(strURL, dwType, strServer, strObject, wPort);
    if (!bResult)
        return FALSE;
    CFile file;
    try
    {
        if (!file.Open(strLocalFileName, CFile::shareDenyNone | CFile::modeRead))
            return FALSE;
        dwFileLength = file.GetLength();
        if (dwFileLength <= 0)
            return FALSE;
        pFileBuff = new char[dwFileLength];
        memset(pFileBuff, 0, sizeof(char) * dwFileLength);
        file.Read(pFileBuff, dwFileLength);

        const int nTimeOut = 5000;
        cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //联接超时设置
        cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);  //重试1次
        pHC = cis.GetHttpConnection(strServer, wPort);  //取得一个Http联接

        pHF = pHC->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
        if (!pHF->SendRequest(NULL, 0, pFileBuff, dwFileLength))
        {
            delete[]pFileBuff;
            pFileBuff = NULL;
            pHF->Close();
            pHC->Close();
            cis.Close();
            return FALSE;
        }
        DWORD dwStateCode = 0;
        pHF->QueryInfoStatusCode(dwStateCode);

        if (dwStateCode == HTTP_STATUS_OK)
            bResult = TRUE;
    }
    catch (...)
    {
        AfxMessageBox((CString)"文件上传出现异常");
    }

    delete[]pFileBuff;
    pFileBuff = NULL;
    file.Close();
    pHF->Close();
    pHC->Close();
    cis.Close();
    return bResult;
}
Csdn user default icon
上传中...
上传图片
插入图片