Note that there are some explanatory texts on larger screens.

plurals
  1. POMFC C++ file upload with http post returning invalid win apis
    primarykey
    data
    text
    <p>I am trying to upload a file using MFC C++ but for some reason i am getting invalid file on the upload. I feels like maybe this is the problem due to wrong header or post information i am using but after several hours tries i cant find my error. Here is my code.. I will appreciate if you experts can elaborate some light on my mistakes so i can correct it...</p> <pre><code>void CFileUpload::UploadByPost(CString strFileName,CString strServerUrl,CString strServerUploadFile) { DWORD dwTotalRequestLength; DWORD dwChunkLength; DWORD dwReadLength; DWORD dwResponseLength; CHttpFile* pHTTP = NULL; dwChunkLength = 64 * 1024; void* pBuffer = malloc(dwChunkLength); CFile file ; if (!file.Open(strFileName.GetBuffer(), CFile::modeRead | CFile::shareDenyWrite)) { return; } CInternetSession session(L"sendFile"); CHttpConnection *connection = NULL; try { //Create the multi-part form data that goes before and after the actual file upload. CString strHTTPBoundary = _T("FFF3F395A90B452BB8BEDC878DDBD152"); CString strPreFileData = MakePreFileData(strHTTPBoundary, file.GetFileName()); CString strPostFileData = MakePostFileData(strHTTPBoundary); CString strRequestHeaders = MakeRequestHeaders(strHTTPBoundary); dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + file.GetLength(); connection = session.GetHttpConnection(/*L"www.YOURSITE.com"*/strServerUrl.GetBuffer(),NULL,INTERNET_DEFAULT_HTTP_PORT); pHTTP = connection-&gt;OpenRequest(CHttpConnection::HTTP_VERB_POST, strServerUploadFile.GetBuffer());//_T("/YOUURL/submit_file.pl")); pHTTP-&gt;AddRequestHeaders(strRequestHeaders); pHTTP-&gt;SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE); //Write out the headers and the form variables pHTTP-&gt;Write((LPSTR)(LPCSTR)strPreFileData.GetBuffer(), strPreFileData.GetLength()); //upload the file. dwReadLength = -1; int length = file.GetLength(); //used to calculate percentage complete. while (0 != dwReadLength) { dwReadLength = file.Read(pBuffer, dwChunkLength); if (0 != dwReadLength) { pHTTP-&gt;Write(pBuffer, dwReadLength); } } file.Close(); //Finish the upload. pHTTP-&gt;Write((LPSTR)(LPCSTR)strPostFileData.GetBuffer(), strPostFileData.GetLength()); pHTTP-&gt;EndRequest(HSR_SYNC); //get the response from the server. LPSTR szResponse; CString strResponse; dwResponseLength = pHTTP-&gt;GetLength(); while (0 != dwResponseLength ) { szResponse = (LPSTR)malloc(dwResponseLength + 1); szResponse[dwResponseLength] = '\0'; pHTTP-&gt;Read(szResponse, dwResponseLength); strResponse += szResponse; free(szResponse); dwResponseLength = pHTTP-&gt;GetLength(); } TRACE(L"%s",strResponse.GetBuffer()); //close everything up. pHTTP-&gt;Close(); connection-&gt;Close(); session.Close(); } catch(CInternetException* e) { TRACE(L"error: %d \n",e-&gt;m_dwError); } catch(CFileException* e) { TRACE(L"error: %d \n",e-&gt;m_cause); } catch(...) { TRACE(L" unexpected error"); } } </code></pre> <p>here is my header and post function </p> <pre><code>CString CFileUpload::MakeRequestHeaders(CString&amp; strBoundary) { CString strFormat; CString strData; strFormat = _T("Content-Type: multipart/form-data; boundary=%s\r\n"); strData.Format(strFormat, strBoundary); return strData; } CString CFileUpload::MakePreFileData(CString&amp; strBoundary, CString&amp; strFileName) { CString strFormat; CString strData; strFormat += _T("--%s"); strFormat += _T("\r\n"); strFormat += _T("Content-Disposition: form-data; name=\"file\"; filename=\"%s\""); strFormat += _T("\r\n"); strFormat += _T("Content-Type: text/plain"); strFormat += _T("\r\n"); strFormat += _T(" XXXXX "); strFormat += _T("\r\n\r\n"); strData.Format(strFormat, strBoundary,/* m_Name, strBoundary,*/ strFileName); return strData; } CString CFileUpload::MakePostFileData(CString&amp; strBoundary) { CString strFormat; CString strData; strFormat = _T("\r\n"); strFormat += _T("--%s"); strFormat += _T("\r\n"); strFormat += _T("Content-Disposition: form-data; name=\"submit\""); strFormat += _T("\r\n\r\n"); strFormat += _T(""); strFormat += _T("\r\n"); strFormat += _T("--%s--"); strFormat += _T("\r\n"); strData.Format(strFormat, strBoundary, strBoundary); return strData; } </code></pre> <p>It always return invalid file, the file server code i am using is the following </p> <pre><code>&lt;?php $allowedExts = array("log", "txt"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "text/plain") ) &amp;&amp; ($_FILES["file"]["size"] &lt; 20000) &amp;&amp; in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] &gt; 0) { echo "Error: " . $_FILES["file"]["error"] . "&lt;br&gt;"; } else { echo "Upload: " . $_FILES["file"]["name"] . "&lt;br&gt;"; echo "Type: " . $_FILES["file"]["type"] . "&lt;br&gt;"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB&lt;br&gt;"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; } ?&gt; </code></pre> <p>and here is the form</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;form action="upload_file.php" method="post" enctype="multipart/form-data"&gt; &lt;label for="file"&gt;Filename:&lt;/label&gt; &lt;input type="file" name="file" id="file"&gt;&lt;br&gt; &lt;input type="submit" name="submit" value="Submit"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Note:- i cant use boost or poco, curl or any third party library.. just only win32 or mfc.</p> <p>here are the pre make and post make data :</p> <p>header</p> <pre><code>"Content-Type: multipart/form-data; boundary=FFF3F395A90B452BB8BEDC878DDBD152 " </code></pre> <p>Pre File </p> <pre><code>"--FFF3F395A90B452BB8BEDC878DDBD152 Content-Disposition: form-data; name="Filename"; filename="ddd.txt" Content-Type: text/plain Content-Transfer-Encoding: binary " </code></pre> <p>Post File</p> <pre><code>" --FFF3F395A90B452BB8BEDC878DDBD152 Content-Disposition: form-data; name="submit" value="submit" --FFF3F395A90B452BB8BEDC878DDBD152-- " </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload