Azeem Haider
unread,Apr 3, 2015, 9:00:53 AM4/3/15You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
I'm going to upload text file using c++. I'm using this code but this is not working and there is no error in this code. can you give me suggestion why this is not working?
Here is code.
#include "stdafx.h"
#include "iostream"
#include "windows.h"
#include "wininet.h"
#include "tchar.h"
#include "iostream"
#include "string"
#include "sstream"
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "Wininet.lib")
using namespace std;
// Wininet.lib
// ws2_32.lib
int main(){
TCHAR hdrs[] = _T("Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858");
TCHAR frmdata[] = _T("-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents here\r\n-----------------------------7d82751e2bc0858--\r\n");
//char frmdata[] = "...";
LPCTSTR accept[] = {_T("*/*"), NULL};
HINTERNET hSession = InternetOpen(_T("MyAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnect = InternetConnect(hSession, _T("localhost"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"), _T("cpp/uploadfile.php"), NULL, NULL, accept, 0, 1);
BOOL sent = HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), frmdata, _tcslen(frmdata));
//BOOL sent = HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), frmdata, strlen(frmdata));
if(hSession==NULL)
{
cout<<"Error: InternetOpen";
}
if(hConnect==NULL)
{
cout<<"Error: InternetConnect";
}
if(hRequest==NULL)
{
cout<<"Error: HttpOpenRequest";
}
if(!sent)
{
cout<<"Error: HttpSendRequest";
}
DWORD status;
DWORD statusLength = sizeof(status);
HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &statusLength, nullptr);
if (status != HTTP_STATUS_OK) {
cout << "HTTP status: " << status << endl;
}
//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return 0;
}
Here is php file code.
<?php
move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "/FileFolder/");
?>
Thanks for your time.