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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值