【UE】HTTP接口上传文件_文件作为入参

这篇具有很好参考价值的文章主要介绍了【UE】HTTP接口上传文件_文件作为入参。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

需求

假设需要在UE中发送下方接口传输文件

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

省流

使用From-data格式
在请求头Content-Type中加入间隔符Boundary
使用LoadFileToArray()读取文件,并加入分隔符、文件头等内容 转成字节 作为Content

在C++中发送请求

创建BlueprintFunctionLibrary蓝图函数库
对应Build.cs中加入Http模块

PrivateDependencyModuleNames.Add("Http");

增加函数

.h中

UFUNCTION(BlueprintCallable, Category = "Gdoog", meta = (WorldContext = "WorldContextObject"))
	//发送上传文件的请求
	static void UploadFileHttp();
	
UFUNCTION(BlueprintPure, Category = "Gdoog", meta = (WorldContext = "WorldContextObject"))
	//字符串转字节,支持中文
	static TArray<uint8> ConvertStringToBytes(const FString& Data);

.cpp中
函数前改为自己的函数库名
文件读取相关头文件
#include “HAL/PlatformFileManager.h”
#include “Misc/FileHelper.h”

http相关头文件
#include “HttpModule.h”

//字符串转字节,支持中文
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;
}

//发送上传文件的请求
void UG_FileHelperForBpBPLibrary::UploadFileHttp()
{
	const FString FilePath = "E:\\XiTong\\Desktop\\测试文件.xls";//文件路径
	const FString FileName = FPaths::GetCleanFilename(FilePath);//文件名字
	const FString HttpUrl = "127.0.0.1:1024/upload";//请求链接
	const FString HttpVerb = "POST";//请求方式
	const FString KeyOfFile = "file";//请求中文件的key
	const FString TypeOfFile = "application/vnd.ms-excel";//文件类型对应的type
	TMap<FString,FString> OtherInfo={{"Key1","Value1"},{"Key2","Value2"}};//其他请求参数
	TArray<uint8> FileContentBytes;//文件内容
	FFileHelper::LoadFileToArray(FileContentBytes, *FilePath);

	//随便创建一个间隔符
	const FString BoundaryLabel = FString(TEXT("e543322540af456f9a3773049ca02529-")) + FString::FromInt(FMath::Rand());
	//每个参数的头部都需要增加的  间隔符
	const FString BoundaryBegin = FString(TEXT("--")) + BoundaryLabel + FString(TEXT("\r\n"));
	//结束时需要增加的   间隔符
	const FString BoundaryEnd = FString(TEXT("\r\n--")) + BoundaryLabel + FString(TEXT("--\r\n"));


	//处理请求内容
	TArray<uint8> ResContent;
	
	/*   文件内容前	例	\r\n
	 *					--e543322540af456f9a3773049ca02529-183\r\n
	 *					Content-Disposition: form-data; name="file"; filename="测试文件.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";

	//加入文件前内容
	ResContent.Append(ConvertStringToBytes(BeginFileString));
	//加入文件内容
	ResContent.Append(FileContentBytes);
	
	/*		加入其他参数   例	\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));
	}

	//加入结尾分隔符
	ResContent.Append(ConvertStringToBytes(BoundaryEnd));

	//创建请求
	FHttpModule& HttpModule = FHttpModule::Get();
	TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = HttpModule.CreateRequest();
	HttpRequest->SetURL(HttpUrl);
	HttpRequest->SetVerb(HttpVerb);
	//替换请求头中的type,加入间隔符
	HttpRequest->SetHeader(TEXT("Content-Type"), FString(TEXT("multipart/form-data; boundary=")) + BoundaryLabel);
	//请求内容
	HttpRequest->SetContent(ResContent);

	//请求回调,按需处理
	// HttpRequest->OnProcessRequestComplete().BindLambda([this](UE_LOG(LogTemp, Error, TEXT("Connection.")););

	//发送
	HttpRequest->ProcessRequest();
}

其中
const FString TypeOfFile = “application/vnd.ms-excel”;//文件类型对应的type
可以对照这个表去设置
常见 content-type对应表https://blog.csdn.net/xiaoyu19910321/article/details/79279364

函数的变量可以 按需 暴露给蓝图
ue http上传文件,http,ue4,ue5

把入参转为字节配合其他节点

如果有使用插件的节点来请求http,比如这里使用的HttpLibrary插件,只需要构建Content,然后在请求头中的Content_Type加入分隔符即可
ue http上传文件,http,ue4,ue5
构建过程与上文相同,多了一步返回分隔符的步骤

UFUNCTION(BlueprintCallable, Category = "Gdoog|File|Http", meta = (DisplayName = "LoadFileToParameter", AutoCreateRefTerm="OtherInfo"))
	//将文件转化为入参,返回分隔符
	static bool LoadFileToParameter_G(const FString FileKey, const FString FilePath, const FString FileType, const TMap<FString, FString>& OtherInfo,TArray<uint8>& Result,FString& Boundary);
//将文件转化为入参
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
需要额外注意使用的插件能否替换请求头中的Content_Type
作者使用的插件 在选择FromData之后是无法替换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对应表文章来源地址https://www.toymoban.com/news/detail-776696.html

到了这里,关于【UE】HTTP接口上传文件_文件作为入参的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Java实现方法接口入参同时包含文件、字段和对象等多种类型。HTTP请求返回415状态,Content type ‘application/octet-stream‘ not supported错误。

    方法一:对象不使用注解 使用Postman测试,直接将subject对象的字段填在key的位置 方法二:对象使用注解@RequestPart 使用Postman测试,将字段包装在subject对象里,使用Content type:application/json的内容类型 注:方法二在开发本地测试执行成功,但是在测试人员机子下不通过,执行报

    2024年02月12日
    浏览(32)
  • JAVA Http接口获取文件下载流,将下载的文件上传阿里云

     需要根据,业务数据,将存在第三方平台的数据,下载至本地,或转存阿里云OSS中。

    2024年02月16日
    浏览(30)
  • 调用hutool包调用http接口处理文件流-文件的上传下载工具类

    hutool工具类get请求获取流: InputStream inputStream = HttpRequest.get(fileUrl).execute().bodyStream(); hutool工具类post请求上传文件流: String resp = HttpRequest.post(url).header(Header.CONTENT_TYPE.getValue(), ContentType.MULTIPART.getValue()).form(params).execute().body(); 完成代码

    2024年01月17日
    浏览(43)
  • 如何将skp文件中的模型导入到UE4中

    首先,skp文件是不能直接导入到UE4中的。 解决方法:用SketchUp(草图大师)打开skp文件,在SketchUp中将模型转换成fbx格式,然后再将模型导入UE4中 目录 1.打开skp文件 2.将skp文件导出为fbx格式 3.将fbx文件导入UE4中 1.打开skp文件         略(双击打开即可) 2.将skp文件导出为

    2024年02月04日
    浏览(32)
  • 史上最全面的UE4 文件操作,打开,读、写,增、删、改、查

    创建一个C++项目,并且创建一个C++蓝图库函数,并且加入头文件 #include \\\"HAL/PlatformFilemanager.h\\\" #include \\\"Misc/FileHelper.h\\\" #include \\\"Misc/Paths.h\\\" #include \\\"Developer/DesktopPlatform/Public/DesktopPlatformModule.h\\\" #include \\\"Developer/DesktopPlatform/Public/IDesktopPlatform.h\\\" #include \\\"Runtime/Core/Public/HAL/FileManagerGeneric.h

    2023年04月09日
    浏览(33)
  • UE4 初始化全局着色器库所需的游戏文件缺失

    ​ 解决的方法是打包的主场景牵扯到的子场景放到同一个文件夹下 ​ ​ 如果移动了场景,会出现一个同名的壳(node),如果还改名了即使在一个文件夹下也会导致这个问题,解决办法是移动完之后右键Content点击fix up···

    2024年02月11日
    浏览(38)
  • UE5 C++自定义Http节点获得Header数据

    一、新建C++文件   选择All Classes,选择父类BlueprintFunctionLibrary,命名为SendHttpRequest。 添加Http支持 代理回调的参数使用DECLARE_DYNAMIC_DELEGATE_TwoParam定义,第一参数是代理类型,后面是参数1类型,参数1,参数2类型,参数2。 代理通过UPROPERTY声明 UFUNCTION的BlueprintCallable是定义一个

    2024年02月04日
    浏览(30)
  • VS2022(V17.6.4)编译UE4源码配置文件(源码包含自编译CEF)

    https://note.youdao.com/s/BwQ80dXk

    2024年02月08日
    浏览(26)
  • 【程序员必备】UE4 C++ 虚幻引擎:详解JSON文件读、写、解析,打造高效开发!

    🙋‍♂️ 作者:海码007 📜 专栏:UE虚幻引擎专栏 💥 标题:【程序员必备】UE4 C++ 虚幻引擎:详解JSON文件读、写、解析,打造高效开发! ❣️ 寄语:人生的意义或许可以发挥自己全部的潜力,所以加油吧! 🎈 最后: 文章作者技术和水平有限,如果文中出现错误,希望大

    2024年02月03日
    浏览(39)
  • 使用Postman之上一个接口的返回值作为下一个接口的入参

    在使用Postman做接口测试的时候,在多个接口的测试中,如果需要上一个接口的返回值作为下一个接口的入参,其基本思路是: 1、获取上一个接口的返回值 2、将返回值设置成环境变量或者全局变量 3、设置下一个接口的参数形式 下面我们来举例说明。 存在两个接口(设置微

    2024年02月06日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包