国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【UE】HTTP接口上傳文件_文件作為入?yún)?/h1>

這篇具有很好參考價(jià)值的文章主要介紹了【UE】HTTP接口上傳文件_文件作為入?yún)?。希望?duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

需求

假設(shè)需要在UE中發(fā)送下方接口傳輸文件

ue http上傳文件,http,ue4,ue5

省流

使用From-data格式
在請(qǐng)求頭Content-Type中加入間隔符Boundary
使用LoadFileToArray()讀取文件,并加入分隔符、文件頭等內(nèi)容 轉(zhuǎn)成字節(jié) 作為Content

在C++中發(fā)送請(qǐng)求

創(chuàng)建BlueprintFunctionLibrary藍(lán)圖函數(shù)庫(kù)
對(duì)應(yīng)Build.cs中加入Http模塊

PrivateDependencyModuleNames.Add("Http");

增加函數(shù)

.h中

UFUNCTION(BlueprintCallable, Category = "Gdoog", meta = (WorldContext = "WorldContextObject"))
	//發(fā)送上傳文件的請(qǐng)求
	static void UploadFileHttp();
	
UFUNCTION(BlueprintPure, Category = "Gdoog", meta = (WorldContext = "WorldContextObject"))
	//字符串轉(zhuǎn)字節(jié),支持中文
	static TArray<uint8> ConvertStringToBytes(const FString& Data);

.cpp中
函數(shù)前改為自己的函數(shù)庫(kù)名
文件讀取相關(guān)頭文件
#include “HAL/PlatformFileManager.h”
#include “Misc/FileHelper.h”

http相關(guān)頭文件
#include “HttpModule.h”

//字符串轉(zhuǎn)字節(jié),支持中文
TArray<uint8> UG_FileHelperForBpBPLibrary::ConvertStringToBytes(const FString& Data)
{
	TArray<uint8> ResBytes;
	FTCHARToUTF8 Converter(*Data);
	ResBytes.SetNum(Converter.Length());
	FMemory::Memcpy(ResBytes.GetData(), (uint8*)(ANSICHAR*)Converter.Get(), ResBytes.Num());
	return ResBytes;
}

//發(fā)送上傳文件的請(qǐng)求
void UG_FileHelperForBpBPLibrary::UploadFileHttp()
{
	const FString FilePath = "E:\\XiTong\\Desktop\\測(cè)試文件.xls";//文件路徑
	const FString FileName = FPaths::GetCleanFilename(FilePath);//文件名字
	const FString HttpUrl = "127.0.0.1:1024/upload";//請(qǐng)求鏈接
	const FString HttpVerb = "POST";//請(qǐng)求方式
	const FString KeyOfFile = "file";//請(qǐng)求中文件的key
	const FString TypeOfFile = "application/vnd.ms-excel";//文件類型對(duì)應(yīng)的type
	TMap<FString,FString> OtherInfo={{"Key1","Value1"},{"Key2","Value2"}};//其他請(qǐng)求參數(shù)
	TArray<uint8> FileContentBytes;//文件內(nèi)容
	FFileHelper::LoadFileToArray(FileContentBytes, *FilePath);

	//隨便創(chuàng)建一個(gè)間隔符
	const FString BoundaryLabel = FString(TEXT("e543322540af456f9a3773049ca02529-")) + FString::FromInt(FMath::Rand());
	//每個(gè)參數(shù)的頭部都需要增加的  間隔符
	const FString BoundaryBegin = FString(TEXT("--")) + BoundaryLabel + FString(TEXT("\r\n"));
	//結(jié)束時(shí)需要增加的   間隔符
	const FString BoundaryEnd = FString(TEXT("\r\n--")) + BoundaryLabel + FString(TEXT("--\r\n"));


	//處理請(qǐng)求內(nèi)容
	TArray<uint8> ResContent;
	
	/*   文件內(nèi)容前	例	\r\n
	 *					--e543322540af456f9a3773049ca02529-183\r\n
	 *					Content-Disposition: form-data; name="file"; filename="測(cè)試文件.xls"\r\n
	 *					Content-Type: application/vnd.ms-excel\r\n\r\n
	*/
	const FString BeginFileString =
		"\r\n"
		+ BoundaryBegin
		+ "Content-Disposition: form-data; name=\"" + KeyOfFile + "\"; filename=\"" + FileName + "\"\r\n"
		+ "Content-Type: " + TypeOfFile + "\r\n\r\n";

	//加入文件前內(nèi)容
	ResContent.Append(ConvertStringToBytes(BeginFileString));
	//加入文件內(nèi)容
	ResContent.Append(FileContentBytes);
	
	/*		加入其他參數(shù)   例	\r\n
	 *						--e543322540af456f9a3773049ca02529-183\r\n
	 *						Content-Disposition: form-data; name="Key1"\r\n\r\n
	 *						Value1
	 */
	for (TPair<FString, FString> Info : OtherInfo)
	{
		ResContent.Append(ConvertStringToBytes(
			"\r\n"
			+ BoundaryBegin
			+ "Content-Disposition: form-data; name=\""+ Info.Key+"\"\r\n\r\n"
			+ Info.Value));
	}

	//加入結(jié)尾分隔符
	ResContent.Append(ConvertStringToBytes(BoundaryEnd));

	//創(chuàng)建請(qǐng)求
	FHttpModule& HttpModule = FHttpModule::Get();
	TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = HttpModule.CreateRequest();
	HttpRequest->SetURL(HttpUrl);
	HttpRequest->SetVerb(HttpVerb);
	//替換請(qǐng)求頭中的type,加入間隔符
	HttpRequest->SetHeader(TEXT("Content-Type"), FString(TEXT("multipart/form-data; boundary=")) + BoundaryLabel);
	//請(qǐng)求內(nèi)容
	HttpRequest->SetContent(ResContent);

	//請(qǐng)求回調(diào),按需處理
	// HttpRequest->OnProcessRequestComplete().BindLambda([this](UE_LOG(LogTemp, Error, TEXT("Connection.")););

	//發(fā)送
	HttpRequest->ProcessRequest();
}

其中
const FString TypeOfFile = “application/vnd.ms-excel”;//文件類型對(duì)應(yīng)的type
可以對(duì)照這個(gè)表去設(shè)置
常見 content-type對(duì)應(yīng)表https://blog.csdn.net/xiaoyu19910321/article/details/79279364

函數(shù)的變量可以 按需 暴露給藍(lán)圖
ue http上傳文件,http,ue4,ue5

把入?yún)⑥D(zhuǎn)為字節(jié)配合其他節(jié)點(diǎn)

如果有使用插件的節(jié)點(diǎn)來(lái)請(qǐng)求http,比如這里使用的HttpLibrary插件,只需要構(gòu)建Content,然后在請(qǐng)求頭中的Content_Type加入分隔符即可
ue http上傳文件,http,ue4,ue5
構(gòu)建過(guò)程與上文相同,多了一步返回分隔符的步驟

UFUNCTION(BlueprintCallable, Category = "Gdoog|File|Http", meta = (DisplayName = "LoadFileToParameter", AutoCreateRefTerm="OtherInfo"))
	//將文件轉(zhuǎn)化為入?yún)?返回分隔符
	static bool LoadFileToParameter_G(const FString FileKey, const FString FilePath, const FString FileType, const TMap<FString, FString>& OtherInfo,TArray<uint8>& Result,FString& Boundary);
//將文件轉(zhuǎn)化為入?yún)?/span>
bool UG_FileHelperForBpBPLibrary::LoadFileToParameter_G(const FString FileKey, const FString FilePath, const FString FileType, const TMap<FString, FString>& OtherInfo,TArray<uint8>& Result,FString& Boundary)
{
	//Preprocess some values    Return Boundary
	Boundary = FString(TEXT("e543322540af456f9a3773049ca02529-")) + FString::FromInt(FMath::Rand());
	const FString BoundaryBegin = FString(TEXT("--")) + Boundary + FString(TEXT("\r\n"));
	const FString BoundaryEnd = FString(TEXT("\r\n--")) + Boundary + FString(TEXT("--\r\n"));
	TArray<uint8> FileBytes;
	FFileHelper::LoadFileToArray(FileBytes, *FilePath);


	//Build content
	Result.Append(ConvertStringToBytes(
		"\r\n"
		+ BoundaryBegin
		+ "Content-Disposition: form-data; name=\"" + FileKey + "\"; filename=\"" + FilePath + "\"\r\n"
		+ "Content-Type: " + FileType + "\r\n\r\n"));
	Result.Append(FileBytes);
	for (TPair<FString, FString> Info : OtherInfo)
	{
		Result.Append(ConvertStringToBytes(
			"\r\n"
			+ BoundaryBegin
			+ "Content-Disposition: form-data; name=\"" + Info.Key + "\"\r\n\r\n"
			+ Info.Value));
	}
	Result.Append(ConvertStringToBytes_G(BoundaryEnd));

	return true;
}

最后效果
ue http上傳文件,http,ue4,ue5
需要額外注意使用的插件能否替換請(qǐng)求頭中的Content_Type
作者使用的插件 在選擇FromData之后是無(wú)法替換Content-Type的
ue http上傳文件,http,ue4,ue5
修改判斷條件 使其允許修改
ue http上傳文件,http,ue4,ue5

參考鏈接

Upload an image using HTTP POST request C++
UE4如何上傳文件
ue4/ue5 Http上傳文件
常見 content-type對(duì)應(yīng)表文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-776696.html

到了這里,關(guān)于【UE】HTTP接口上傳文件_文件作為入?yún)⒌奈恼戮徒榻B完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包