当前位置:   article > 正文

UE Http下载文件/解压压缩包_ue5 http下载一个文件

ue5 http下载一个文件

如果有大佬知道怎么获取下载文件如何获取自身命名麻烦告知一下。谢谢。

下载

 

  1. .h
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "Kismet/BlueprintAsyncActionBase.h"
  5. #include "Interfaces/IHttpRequest.h"
  6. #include "AsyncHttpObject.generated.h"
  7. #pragma region One
  8. DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FDownloadProgressDelegate,
  9. const int32&, ReceivedDataInBytes,
  10. const int32&, TotalDataInBytes,
  11. const TArray<uint8>&, BinaryData
  12. );
  13. DECLARE_DYNAMIC_MULTICAST_DELEGATE(FDownloadDelegate);
  14. #pragma endregion
  15. /**
  16. *
  17. */
  18. UCLASS()
  19. class PIECETOGETHERTOOLS_API UAsyncHttpObject : public UBlueprintAsyncActionBase
  20. {
  21. GENERATED_BODY()
  22. UFUNCTION(BlueprintCallable, Category = "Vince | AsyncHttp", meta = (BlueprintInternalUseOnly = "true"))
  23. static UAsyncHttpObject* AsyncHttpDownload(const FString& url,FString fileSavePath, FString ileSaveName);
  24. void DownloadRequestStart(const FString& url);
  25. void DownloadRequestHandle(FHttpRequestPtr request, FHttpResponsePtr response, bool bWasSuccessful);
  26. void DownloadProgress(FHttpRequestPtr request, int32 bytesSent, int32 bytesReceived);
  27. //输出节点
  28. UPROPERTY(BlueprintAssignable, Category = "Vince | AsyncHttp")
  29. FDownloadProgressDelegate OnProgressUpdate; //最好把有参数的放在前面,不然参数在蓝图节点有时候被吃掉了
  30. //输出节点
  31. UPROPERTY(BlueprintAssignable, Category = "Vince | AsyncHttp")
  32. FDownloadDelegate OnSucc;
  33. //输出节点
  34. UPROPERTY(BlueprintAssignable, Category = "Vince | AsyncHttp")
  35. FDownloadDelegate OnFailed;
  36. };
  1. .cpp
  2. #include "AsyncHttpObject.h"
  3. #include "HttpModule.h"
  4. #include "Interfaces/IHttpResponse.h"
  5. //存储目录
  6. FString FileSavePath =TEXT("");
  7. //存储名字
  8. FString FileSaveName = TEXT("");
  9. UAsyncHttpObject* UAsyncHttpObject::AsyncHttpDownload(const FString& url, FString fileSavePath, FString fileSaveName)
  10. {
  11. UAsyncHttpObject* AsyncHttpObject = NewObject<UAsyncHttpObject>();
  12. AsyncHttpObject->DownloadRequestStart(url);
  13. FileSavePath = fileSavePath;
  14. FileSaveName = fileSaveName;
  15. return AsyncHttpObject;
  16. }
  17. //开始请求下载
  18. void UAsyncHttpObject::DownloadRequestStart(const FString& url)
  19. {
  20. TSharedRef<IHttpRequest, ESPMode::ThreadSafe> request = FHttpModule::Get().CreateRequest();
  21. request->SetVerb("GET");
  22. request->SetURL(url);
  23. request->OnProcessRequestComplete().BindUObject(this, &UAsyncHttpObject::DownloadRequestHandle); //请求回调
  24. request->OnRequestProgress().BindUObject(this, &UAsyncHttpObject::DownloadProgress);// 下载进度
  25. request->ProcessRequest();
  26. RemoveFromRoot(); // 手动GC
  27. }
  28. 请求回调
  29. void UAsyncHttpObject::DownloadRequestHandle(FHttpRequestPtr request, FHttpResponsePtr response, bool bWasSuccessful)
  30. {
  31. RemoveFromRoot();
  32. if (bWasSuccessful && response.IsValid() && EHttpResponseCodes::IsOk(response->GetResponseCode()))
  33. {
  34. if (response->GetContentLength() > 0) {
  35. TArray<uint8> EmptyData;
  36. OnProgressUpdate.Broadcast(response->GetContentLength(), response->GetContentLength(), EmptyData);
  37. }
  38. //FString FileSavePath = FPaths::ProjectDir() + "/download/gameplay.png";
  39. //下载返回类型设置
  40. FString sType, SLeft, SRight;
  41. sType = response->GetHeader(TEXT("Content-Type"));
  42. sType.Split("/",&SLeft,&SRight);
  43. FileSaveName.Append(FString::Printf(TEXT(".%s"), *SRight)) ;
  44. //组合 存储地址
  45. FileSavePath = FPaths::Combine(FileSavePath, FileSaveName);
  46. FString Path, Filename, Extension;
  47. FPaths::Split(FileSavePath, Path, Filename, Extension);
  48. IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
  49. if (!PlatformFile.DirectoryExists(*Path))
  50. {
  51. if (!PlatformFile.CreateDirectoryTree(*Path))
  52. {
  53. UE_LOG(LogTemp, Error, TEXT("Create Directory Failed!"));
  54. OnFailed.Broadcast();
  55. return;
  56. }
  57. }
  58. IFileHandle* FileHandle = PlatformFile.OpenWrite(*FileSavePath);
  59. if (FileHandle)
  60. {
  61. FileHandle->Write(response->GetContent().GetData(), response->GetContentLength());
  62. delete FileHandle;
  63. OnSucc.Broadcast();
  64. return;
  65. }
  66. else {
  67. UE_LOG(LogTemp, Error, TEXT("Save File Failed!"));
  68. OnFailed.Broadcast();
  69. return;
  70. }
  71. }
  72. UE_LOG(LogTemp, Error, TEXT("download File Failed!"));
  73. OnFailed.Broadcast();
  74. return;
  75. }
  76. // 下载进度
  77. void UAsyncHttpObject::DownloadProgress(FHttpRequestPtr request, int32 bytesSent, int32 bytesReceived)
  78. {
  79. if (request->GetResponse()->GetContentLength() > 0)
  80. {
  81. TArray<uint8> EmptyData;
  82. OnProgressUpdate.Broadcast(bytesReceived, request->GetResponse()->GetContentLength(), EmptyData);
  83. }
  84. }

官方免费下载插件

也不知道怎么自动设置名字和后缀,可能有大佬知道怎么"微操".还望分享.这个下载进度的回调是正常的.我自己改的那个下载进度回调有问题.

在代码插件创建的Runtime Files Downloader - 虚幻引擎商城

​​​​​​​

解压zip

官方免费插件
Runtime Archiver

在代码插件创建的Runtime Archiver - 虚幻引擎商城

示例

*注意上面第一个变量必须指定文件.不然会失败

结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/91244
推荐阅读
相关标签
  

闽ICP备14008679号