UE4上传图片和PHP交互

 

#include "IHttpRequest.h"
#include "HttpModule.h"
#include "Base64.h"
"HTTP"
void AMyActor::MyHttpCall(FString TexturePath)
{
	// TexturePath contains the local file full path
	//FString TexturePath = TEXT("H:/SVN/VRHome/Saved/Screenshots/Windows/0.jpg");
	// file name
	int32 LastSlashPos;
	TexturePath.FindLastChar('/', LastSlashPos);
	FString FileName = TexturePath.RightChop(LastSlashPos + 1);

	// get data
	TArray<uint8> UpFileRawData;
	FFileHelper::LoadFileToArray(UpFileRawData, *TexturePath);

	FString JsonStr;
	TSharedRef< TJsonWriter<TCHAR, TCondensedJsonPrintPolicy<TCHAR> > > JsonWriter = TJsonWriterFactory<TCHAR, TCondensedJsonPrintPolicy<TCHAR> >::Create(&JsonStr);
	JsonWriter->WriteObjectStart();
	JsonWriter->WriteValue(TEXT("FileToUpload"), TEXT("FileName"));
	JsonWriter->WriteValue(TEXT("ImageData"), FBase64::Encode(UpFileRawData));
	JsonWriter->WriteObjectEnd();
	// Close the writer and finalize the output such that JsonStr has what we want
	JsonWriter->Close();

	TSharedRef<IHttpRequest> Request = FHttpModule::Get().CreateRequest();

	Request->SetHeader(TEXT("Content-Type"), TEXT("application/json;charset=utf-8"));
	Request->SetURL(TEXT("http://localhost/LoadTexture.php"));
	Request->SetVerb(TEXT("POST"));
	Request->SetContentAsString(JsonStr);
	Request->ProcessRequest();
	//Request->OnProcessRequestComplete().BindUObject(this, &AMyActor::AAA);
	//GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, value);//prints nothing
}

PHP:

 

 

<?php

// 首先接收上传的数据

$post_data = file_get_contents('php://input');

// 解析json字符串

$obj = json_decode($post_data);

// 获取包含在Json字符串中的数据

// echo $obj->{'ImageData'};

$myfile=fopen("newfile.png", "w") or die("Unable to open file!");

$txt = base64_decode($obj->{'ImageData'});

fwrite($myfile,$txt);

echo "成功啦!";
?>

 

原文地址:http://www.cnblogs.com/staticmao/p/6289449.html

参考:https://wiki.unrealengine.com/UE4%E4%B8%8EWEB%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%BA%A4%E4%BA%92(json)

可参考:https://zhuanlan.zhihu.com/p/79216048

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页