zoukankan      html  css  js  c++  java
  • MFC之HTTP文件上传

    BOOL UploadFile(LPCTSTR strURL, LPCTSTR strLocalFileName)
    {
    // 如果URL为空或者文件不存在,直接返回
    	if (strURL == NULL ||
    		strURL == _T("") ||
    		strLocalFileName == NULL ||
    		strLocalFileName == _T("") ||
    		!PathFileExists(strLocalFileName) ||
    		PathIsDirectory(strLocalFileName))
    	{
    		return FALSE;
    	}
    
    	CHttpFile *pHttpFile = NULL;
    	CHttpConnection* connection = NULL;
    	CInternetSession session(_T("UploadFile"));
    
    	DWORD dwType;
    	INTERNET_PORT nPort;
    	CString strServer, strObject;
    	if (!AfxParseURL(strURL, dwType, strServer, strObject, nPort)) return FALSE;
    
    	BOOL bSuccess = FALSE;
    	DWORD dwReadLength;
    	DWORD dwResponseLength;
    	DWORD dwTotalRequestLength;
    	const DWORD dwChunkLength = 1024;
    
    	char pBuffer[dwChunkLength];
    	memset(pBuffer, 0, dwChunkLength);
    
    	try
    	{
    		connection = session.GetHttpConnection(strServer, nPort);
    		pHttpFile = connection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
    
    		CTime ct;
    		CString strTime, strBoundary;
    		ct = CTime::GetCurrentTime();
    		strTime = ct.Format(_T("%Y%m%d%H%M%S"));
    		strBoundary = _T("*-*-*") + strTime + _T("*-*-*");
    
    		CString strPreFileHeaders;
    		strPreFileHeaders.Format(_T("Content-Type: multipart/form-data; boundary=%s
    "), strBoundary);
    		pHttpFile->AddRequestHeaders(strPreFileHeaders);
    
    		CString strPostData;
    		strPostData.Format(_T("--%s
    Content-Disposition: form-data; name ="voice"; filename="%s.wav"
    Content-Type: audio/x-wav
    
    "), strBoundary, strTime);
    		CString endPostData;
    		endPostData.Format(_T("
    --%s--
    "), strBoundary);
    
    		CFile file;
    		file.Open(strLocalFileName, CFile::shareDenyNone | CFile::modeRead);
    
    		DWORD sendLength = strPostData.GetLength() + endPostData.GetLength() + file.GetLength();
    		pHttpFile->SendRequestEx(sendLength, HSR_SYNC | HSR_INITIATE);
    
    		pHttpFile->Write((LPSTR)(LPCTSTR)strPostData, strPostData.GetLength());
    		dwReadLength = -1;
    		while (0 != dwReadLength)
    		{
    			dwReadLength = file.Read(pBuffer, dwChunkLength);
    			if (dwReadLength > 0)
    			{
    				pHttpFile->Write(pBuffer, dwReadLength);
    			}
    		}
    		file.Close();
    
    		pHttpFile->Write((LPSTR)(LPCTSTR)endPostData, endPostData.GetLength());
    		pHttpFile->EndRequest();
    
    		DWORD dwStauts;
    		if (!pHttpFile->QueryInfoStatusCode(dwStauts) ||
    			dwStauts < 200 || 299 < dwStauts)
    		{
    			bSuccess = FALSE;
    		}
    		else
    		{
    			CString response, text;
    			for (int i = 0; pHttpFile->ReadString(text); ++i)
    			{
    				response += text + "
    ";
    			}
    			if (response.IsEmpty())
    			{
    				bSuccess = FALSE;
    			}
    			else
    			{
    				MPP::Json json = MPP::Json::Parse(response);
    				bSuccess = json.Contains("state") && !json["state"].IsNull() &&
    					json["state"].IsBool() && json["state"].AsBool();
    			}
    		}
    	}
    	catch (...)
    	{
    		bSuccess = FALSE;
    	}
    
    	if (pHttpFile != NULL)
    	{
    		pHttpFile->Close();
    		delete pHttpFile;
    		pHttpFile = NULL;
    	}
    	if (connection != NULL)
    	{
    		connection->Close();
    		delete connection;
    		connection = NULL;
    	}
    	session.Close();
    	return bSuccess;
    }
    

      

  • 相关阅读:
    耿建超英语语法---使动词
    tensorboard的安装及遇到的问题
    利用PIL实现图片的切割
    mysql explain
    laravel sql查询
    聚簇索引和非聚簇索引
    修改数据表结构导致的问题
    接口优化记录
    redis优化记录
    php 判断两个数组是否相等
  • 原文地址:https://www.cnblogs.com/mupiaomiao/p/6402681.html
Copyright © 2011-2022 走看看