ホーム » MFC » CHttpFile を使用したアップロード

2017年9月
 12
3456789
10111213141516
17181920212223
24252627282930

カテゴリー

アーカイブ

ブログ統計情報

  • 77,342 アクセス



CHttpFile を使用したアップロード

CHttpFile を使用したアップロードのコードを整理.
サーバ側は先日の php と同様.
https://jml.mish.work/index.php/i-tools/upload-htm-php.html


//*******************************************************************************
//	send request define
//	Create	:	2017/08/28
//*******************************************************************************
#define	C_CRLF			_T("\r\n") ;
#define	CT_ct_mp_fd_b_		_T("Content-Type: multipart/form-data; boundary=")
#define	CT_boundary__		_T("--")
#define	CD_cd_f_d_n_		_T("Content-Disposition: form-data; name=")
#define	CD_cd__fn_		_T("; filename=") ;
#define	CT_ct_a_o_s		_T("Content-Type: application/octet-stream")

//*******************************************************************************
//	make send data
//	Create	:	2017/08/28
//*******************************************************************************
inline	v_char	Make_send_data	(LPCTSTR upFile,LPCTSTR ___boundary)
{
	v_char	up_Data = v_c_Load (upFile) ;
	v_char	sndData ;
	{
		tstring	ct_boundary	= ___boundary ;
		tstring	file_img	= ::QuotM_Add(_T("file_img")) ;
		tstring	fileName	= ::QuotM_Add(::Path_GetName(upFile)) ;
		tstring	dataPre ;
		tstring	dataPst ;
		dataPre+=	CT_boundary__	+	ct_boundary				+	C_CRLF ;
		dataPre+=	CD_cd_f_d_n_	+	file_img	+	CD_cd__fn_	;
		dataPre+=							fileName	+	C_CRLF ;
		dataPre+=	T_ct_a_o_s								C_CRLF ;
		dataPre+=										C_CRLF ;
		dataPst+=										C_CRLF ;
		dataPst+=	CT_boundary__	+	 ct_boundary	+	CT_boundary__		C_CRLF ;
		v_char	vc_pref = ::To_v_char(::To__string(dataPre.c_str())) ;
		v_char	vc_post = ::To_v_char(::To__string(dataPst.c_str())) ;
		sndData.insert(sndData.end(),vc_pref.begin(),vc_pref.end()) ;
		sndData.insert(sndData.end(),up_Data.begin(),up_Data.end()) ;
		sndData.insert(sndData.end(),vc_post.begin(),vc_post.end()) ;
		}
	#ifdef	_DEBUG
	{
		::i_Dump(sndData,(::Path_GetName(upFile)+_T(".txt")).c_str()) ;
		}
	#endif
	return	sndData ;
	}

//*******************************************************************************
//	upload
//	Create	:	2017/08/28
//*******************************************************************************
inline	bool	UploadFile	(LPCTSTR svrName,LPCTSTR php,LPCTSTR upFile)
{
	{
		if (::File_IsNothing(upFile))			{	return	false ;		}
	//	if (::File_GetSize  (upFile) > 2048*1024)	{	return	false ;		}
		}
	tstring	head ;
	v_char	sndData ;
	{
		tstring	___boundary = _T("-----UpFile__2017_08_30") ;
			___boundary = _T("-----") + ::Path_GetName(_T(__FILE__)) + _T("__") + ::Now_Format(_T("%H%M%S")) ;
		head	= CT_ct_mp_fd_b_	+ ___boundary ;
		sndData = Make_send_data(upFile,  ___boundary.c_str()) ;
		}
	tstring	serverN = svrName ;
	tstring	portStr ;
	{
		v_tstring	strAry = ::String_Split(svrName,false,_T(":")) ;
		if (strAry.size() >= 2) {
			serverN = strAry[0] ;
			portStr = strAry[1] ;
			}
		}
	INTERNET_PORT	nPort = 0 ;
			nPort = ::ttou2(portStr) ;
	tstring		userAgent = _T("drop_up") ;
			userAgent = ::Path_GetName(_T(__FILE__)).c_str() ;
	#ifdef	_WIN32
	{
		userAgent = ::Path_GetName(::i_GetModuleFileName()) ;
		{
			tstring	osVer	= _T("Windows NT ") + ::To_tstring_rz(::GetWinVer_exe()) ;
				osVer	+=  tstring(_T(" "))+ Bracket_Add(::GetWinVerStr_exe(),_T('(')).c_str() ;
			userAgent += _T(" ") + osVer ;
			userAgent += EXE_AddVerBuildStr() ;
			}
		}
	#endif
	CInternetSession	session(userAgent.c_str()) ;
	CHttpConnection*	pServer = NULL ;
	CHttpFile*		pFile = NULL ;
	DWORD			dwStatus = 0 ;
	try	{
				pServer = session.GetHttpConnection(serverN.c_str(),nPort) ;
				if (pServer == NULL)				{	return	false ;		}
			{
				CString	headStr = head.c_str() ;
				pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,	php) ;
				if (pFile == NULL)				{	return	false ;		}
				pFile->SendRequest(headStr,(LPVOID)(&sndData[0]),DWORD(sndData.size())) ;
				pFile->QueryInfoStatusCode(dwStatus) ;
				}
			{
				delete	(pFile) ;
				delete	(pServer) ;
				}
		}
	catch	(CInternetException* e) {
		CString	errMsg ;
		e->GetErrorMessage(errMsg.GetBuffer(1024),1024) ;
		errMsg.ReleaseBuffer() ;
		std::tout << LPCTSTR(errMsg) << std::endl ;
		return	false ;
		}
	session.Close() ;
	return	true ;
	}

//*******************************************************************************
//	upload files
//	Create	:	2017/08/29
//*******************************************************************************
inline	bool	UploadFiles	(c_v_tstring& upFiles)
{
	v_tstring	svr_php = ::UF_get_server_php() ;
	tstring		serverN = svr_php[0] ;
	tstring		phpName = svr_php[1] ;
	for (size_t index=0 ; index<upFiles.size() ; index++) {
		tstring	upFile = upFiles[index] ;
		if (::File_IsNothing(upFile))		{	continue ;		}
		if (!::UploadFile(serverN.c_str(),phpName.c_str(),upFile.c_str()))	{
			return	false ;
			}
		}
	return	true ;
	}

UploadFile


これらを使用したツールは以下にあります.
http://i_tools.mish.work/2019/10/up-htm.html


2020/09
日本語を含むファイル名のアップロード

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

2件のコメント

コメントする

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

%d人のブロガーが「いいね」をつけました。