Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

upload data using xmlhttp (need urgent help)

$
0
0
hey guys!!!
m newbie to vc++ and trying to upload a file...but my code does't upload file except with extensions txt, php...
moreover cstring doesn't take data more than 512 kb
following is the code:
<code>
#include "stdafx.h"
#include "yxmlhttp.h"
#include<iostream>
#include<fstream>
#include <tchar.h>
#include<afxinet.h>
#import "msxml3.dll"
using namespace MSXML2;
using namespace std;

CString strBoundary = _T("1q2w3e");
CString strFileName("aaa.txt");
CString strPath("C:\\Users\\abc\\Desktop\\");
CFile file;


CString upDir(CString strUploadDir)
{
HINSTANCE hnd=(HINSTANCE)GetModuleHandle(NULL);
char k1[200];
k1[0]='\0';
LoadStringA(hnd,foldr,k1,200);
string a;
char cname[100]; DWORD clength=100;
GetComputerNameA(cname, &clength);a.append(k1);a.append("/");
a.append(cname);//a.append("/");
CString a1(a.c_str());
strUploadDir=a1;
return strUploadDir;
}

CString BUFFER(CString buffer)
{
DWORD dwMaxLength = 64 * 1024;
char* pBuffer = (char*)malloc(dwMaxLength);
DWORD dwReadLength = -1;
int length = file.GetLength();
char *new_chars='\0';
string asa,buff;buff.clear();
while (0 != dwReadLength)
{
dwReadLength = file.Read(pBuffer, dwMaxLength);
if (0 != dwReadLength)
{

buff.append(pBuffer);
asa=buff.substr(0,length);
}


}
free(pBuffer);
buffer= (CString)asa.c_str();
file.Close();

return buffer;
}

CString header(CString strBoundary)
{
CString strUploadDir;
CString buffer = L"";
CString strFormat = L"";
CString strData = L"";
strData.Empty();
CString strUpldDir=upDir(strUploadDir);
//header fr URL
strFormat += BUFFER(buffer);

//footer fr URL

strFormat += _T("\r\n");


strData.Format(strFormat, strBoundary, strUpldDir, strBoundary,strFileName, strBoundary, strBoundary);
return strData;
}




int XMLHttpRequestSample()
{
CoInitialize(NULL);
IXMLHTTPRequestPtr pIXMLHTTPRequest = NULL;
BSTR Response = NULL;


if (!file.Open(strPath + strFileName, CFile::modeRead | CFile::shareDenyWrite))
{
TCHAR szError[1024];
return FALSE;
}

try{
HRESULT hr;

hr=pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP ");//,NULL,CLSCTX_INPROC_SERVER);
SUCCEEDED(hr) ? 0 : throw hr;

hr=pIXMLHTTPRequest->open("POST",URL , false);
SUCCEEDED(hr) ? 0 : throw hr;

hr=pIXMLHTTPRequest->setRequestHeader("Content-Type:", "multipart/form-data; boundary=1q2w3e");


CString strHeader = header(strBoundary);
DWORD headerLen=strHeader.GetLength();

char charMB[50000];
LPCWSTR charFile = strHeader.GetBuffer( headerLen);
int i= wcstombs(charMB, charFile,sizeof(charMB));
const char* charFileData=charMB;

_variant_t v = new VARIANT();
v.SetString(charFileData);
hr=pIXMLHTTPRequest->send(v);
SUCCEEDED(hr) ? 0 : throw hr;
Response = pIXMLHTTPRequest->responseText;

strHeader.ReleaseBuffer( );
}

catch (...) {
MessageBox(NULL, _T("Exception occurred"), _T("Error"), MB_OK);
if(Response)
::SysFreeString(Response);
}

}


int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
XMLHttpRequestSample();
}
</code>
thanx in advance:)

Viewing all articles
Browse latest Browse all 2703

Trending Articles