Sign In Register

How can we help you today?

Start a new topic
Answered

Upload Binary file from UE4

Hi there,


I'm trying to figure out how to upload binary files from UE4 (C++). 


I'm getting the URL with GetUploadURLRequest then passing it on to the function below. But I just get a connection lost message.


 

       SharedRef < IHttpRequest > Request = Http->CreateRequest();
        
        FString TexturePath = FPaths::GameSavedDir() + "\\Landscape.png";
        TArray<uint8> UpFileRawData;
        FFileHelper::LoadFileToArray(UpFileRawData, *TexturePath);
        
        
        Request->SetURL((TEXT("%s"),URL));
        Request->SetVerb("POST");
        Request->SetHeader(TEXT("Content-Type") ,TEXT("multipart/form-data")); //tried image/png also
        Request->SetContent(UpFileRawData);
        
        Request->OnProcessRequestComplete().BindUObject(this, &UFileUploader::OnResponseReceived);
        Request->ProcessRequest();

 

Thanks



Best Answer

Just to add to this. I've tried the code below. I get a 200 response but an "Unable to upload file" message?


  

        TSharedRef < IHttpRequest > Request = Http->CreateRequest();
        
        FString TexturePath = FPaths::GameSavedDir() + "\\Landscape.png";
        // get data
        TArray<uint8> UpFileRawData;
        FFileHelper::LoadFileToArray(UpFileRawData, *TexturePath);
        
        
        Request->SetURL(URL);
        Request->SetVerb("POST");
		Request->SetHeader("Content-Type", "multipart/form-data; boundary=blahblahsomeboundary");
        
        
        
        
		FString a = "\r\n--blahblahsomeboundary\r\n";			
		FString b = "Content-Disposition: form-data; name=\"file\";  filename=\"landscape2.png\"\r\n";
		FString c = "Content-Type: image/png\r\n\r\n";		
        //d = UpFileRawData
		FString e = "\r\n--blahblahsomeboundary--\r\n";
        
        
        FBufferArchive BinaryArrayArchive;
		BinaryArrayArchive << a;		
        BinaryArrayArchive << b;
		BinaryArrayArchive << c;
        BinaryArrayArchive << UpFileRawData;
		BinaryArrayArchive << e;        


		/*
		TArray<uint8> data;
		data.Append((uint8*)TCHAR_TO_UTF8(*a), a.Len());
		data.Append((uint8*)TCHAR_TO_UTF8(*b), b.Len());
		data.Append((uint8*)TCHAR_TO_UTF8(*c), c.Len());
		data.Append(UpFileRawData);
		data.Append((uint8*)TCHAR_TO_UTF8(*e), e.Len());*/
		

        Request->SetContent(BinaryArrayArchive); //or data<uint8>
        
		
        Request->OnProcessRequestComplete().BindUObject(this, &UFileUploader::OnResponseReceived);
        if (!Request->ProcessRequest())

  


Answer

Just to add to this. I've tried the code below. I get a 200 response but an "Unable to upload file" message?


  

        TSharedRef < IHttpRequest > Request = Http->CreateRequest();
        
        FString TexturePath = FPaths::GameSavedDir() + "\\Landscape.png";
        // get data
        TArray<uint8> UpFileRawData;
        FFileHelper::LoadFileToArray(UpFileRawData, *TexturePath);
        
        
        Request->SetURL(URL);
        Request->SetVerb("POST");
		Request->SetHeader("Content-Type", "multipart/form-data; boundary=blahblahsomeboundary");
        
        
        
        
		FString a = "\r\n--blahblahsomeboundary\r\n";			
		FString b = "Content-Disposition: form-data; name=\"file\";  filename=\"landscape2.png\"\r\n";
		FString c = "Content-Type: image/png\r\n\r\n";		
        //d = UpFileRawData
		FString e = "\r\n--blahblahsomeboundary--\r\n";
        
        
        FBufferArchive BinaryArrayArchive;
		BinaryArrayArchive << a;		
        BinaryArrayArchive << b;
		BinaryArrayArchive << c;
        BinaryArrayArchive << UpFileRawData;
		BinaryArrayArchive << e;        


		/*
		TArray<uint8> data;
		data.Append((uint8*)TCHAR_TO_UTF8(*a), a.Len());
		data.Append((uint8*)TCHAR_TO_UTF8(*b), b.Len());
		data.Append((uint8*)TCHAR_TO_UTF8(*c), c.Len());
		data.Append(UpFileRawData);
		data.Append((uint8*)TCHAR_TO_UTF8(*e), e.Len());*/
		

        Request->SetContent(BinaryArrayArchive); //or data<uint8>
        
		
        Request->OnProcessRequestComplete().BindUObject(this, &UFileUploader::OnResponseReceived);
        if (!Request->ProcessRequest())

  

Hi Dan,


Apologies for the slow response, I'll get someone to take a closer look at this for you.


Thanks,

Liam

No Problem! It started working eventually?!. The code is below in case somebody needs it. However, when I was getting errors (the "Unable to upload image" message), it was still returning a 200 OK response. Would it be possible for you to change that to an error code response (500?) so I can easily check for it.


Thanks!


 

Http = &FHttpModule::Get();
    
    if (Http && Http->IsHttpEnabled())
    {
        TSharedRef < IHttpRequest > Request = Http->CreateRequest();
        
        FString TexturePath = FPaths::GameSavedDir() + "\\Landscape.png";
        // get data
        TArray<uint8> UpFileRawData;
        FFileHelper::LoadFileToArray(UpFileRawData, *TexturePath);
        
        
        Request->SetURL(URL);
        Request->SetVerb("POST");
		Request->SetHeader("Content-Type", "multipart/form-data; boundary=blahblahsomeboundary");
        
        
        
        
		FString a = "\r\n--blahblahsomeboundary\r\n";
		FString b = "Content-Disposition: form-data; name=\"file\";  filename=\"landscape2.png\"\r\n";
		
		FString c = "Content-Type: image/png\r\n\r\n";		
        //d = UpFileRawData
		FString e = "\r\n--blahblahsomeboundary--\r\n";
        
        	
		TArray<uint8> data;
		data.Append((uint8*)TCHAR_TO_UTF8(*a), a.Len());
		data.Append((uint8*)TCHAR_TO_UTF8(*b), b.Len());
		data.Append((uint8*)TCHAR_TO_UTF8(*c), c.Len());
		data.Append(UpFileRawData);
		data.Append((uint8*)TCHAR_TO_UTF8(*e), e.Len());
		

        Request->SetContent(data); 
        
		
        Request->OnProcessRequestComplete().BindUObject(this, &UFileUploader::OnResponseReceived);
        if (!Request->ProcessRequest())
        {
            OnFailure.Broadcast();
        }
    }
    else OnFailure.Broadcast();

 



Login to post a comment