locked
The exception when upload file using IXHR2

    Question

  • Hi all,

    I want to upload a file using IXHR2,how to set the request header, I try to use these codes but get a exception in open function.

    	DWORD dwStatus = 0;
    	ULONGLONG ullFileSize = 0;
    	ULONG BytesWritten=0;
    	ComPtr<IXMLHTTPRequest2> spXHR;
    	ComPtr<XMLHTTPRequest2Callback> spMyXhrCallback;
    	ComPtr<IXMLHTTPRequest2Callback> spXhrCallback;
    	//ComPtr<XMLHttpRequestPostStream> spMyXhrPostStream;
    	ComPtr<ISequentialStream> spXhrPostStream;
    	HRESULT hr = S_FALSE;
    	//Initialize IXMLHTTPRequest2Callback
    	MakeAndInitialize<XMLHTTPRequest2Callback>(&spMyXhrCallback);
    	spMyXhrCallback.As(&spXhrCallback);
    
    	// Create an object of the CXMLHttpRequestPostStream class.
    	//spMyXhrPostStream = Make<XMLHttpRequestPostStream>();
    	std::wstring data;
    	data.append(L"-----D514116929C24fAAAE047F2B82C59BAA\r\n");
    	data.append(L"Content-Disposition: form-data; name=\"filename\"\r\n");
    	data.append(L"\r\n");
    	data.append(L"1.txt\r\n");
    	data.append(L"-----D514116929C24fAAAE047F2B82C59BAA");
    	data.append(L"Content-Disposition: form-data; name=\"uploadfile\"; filename=\"1.txt\"\r\n");
    	data.append(L"Content-Type: application/octet-stream\r\n\r\n");
    	data.append(L"abcdefggggggggggg\r\n");
    	data.append(L"-----D514116929C24fAAAE047F2B82C59BAA--");
    
    	//spMyXhrPostStream->Open(L"sample.txt");
    	String^ postData = ref new String(data.c_str());
    	ComPtr<IStream> spXhrPostStr;
    	
    
    	hr = CreateStreamOnHGlobal(nullptr,
    		true,
    		&spXhrPostStr);
    
    	hr =spXhrPostStr->Write(postData->Data(),postData->Length(),&BytesWritten);
    	LongLongToULongLong(BytesWritten,&ullFileSize);
    
    	hr = spXHR->Open(L"POST",               // Method.
    		L"http://localhost:1235/Upload1.aspx",              // Url.
    		NULL,   // Callback.
    		NULL,                  // Username.
    		NULL,                  // Password.
    		NULL,                  // Proxy username.
    		NULL);                 // Proxy password.
    
    	spXHR->Send(spXhrPostStream.Get(), ullFileSize);

    Need help thanks


    NEU_ShieldEdge

    Wednesday, June 27, 2012 9:06 AM

All replies