赞
踩
如果有大佬知道怎么获取下载文件如何获取自身命名麻烦告知一下。谢谢。
- .h
-
- #pragma once
-
- #include "CoreMinimal.h"
- #include "Kismet/BlueprintAsyncActionBase.h"
- #include "Interfaces/IHttpRequest.h"
- #include "AsyncHttpObject.generated.h"
-
- #pragma region One
-
- DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FDownloadProgressDelegate,
- const int32&, ReceivedDataInBytes,
- const int32&, TotalDataInBytes,
- const TArray<uint8>&, BinaryData
- );
- DECLARE_DYNAMIC_MULTICAST_DELEGATE(FDownloadDelegate);
-
- #pragma endregion
-
-
-
- /**
- *
- */
- UCLASS()
- class PIECETOGETHERTOOLS_API UAsyncHttpObject : public UBlueprintAsyncActionBase
- {
- GENERATED_BODY()
-
- UFUNCTION(BlueprintCallable, Category = "Vince | AsyncHttp", meta = (BlueprintInternalUseOnly = "true"))
- static UAsyncHttpObject* AsyncHttpDownload(const FString& url,FString fileSavePath, FString ileSaveName);
-
- void DownloadRequestStart(const FString& url);
- void DownloadRequestHandle(FHttpRequestPtr request, FHttpResponsePtr response, bool bWasSuccessful);
- void DownloadProgress(FHttpRequestPtr request, int32 bytesSent, int32 bytesReceived);
-
- //输出节点
- UPROPERTY(BlueprintAssignable, Category = "Vince | AsyncHttp")
- FDownloadProgressDelegate OnProgressUpdate; //最好把有参数的放在前面,不然参数在蓝图节点有时候被吃掉了
-
- //输出节点
- UPROPERTY(BlueprintAssignable, Category = "Vince | AsyncHttp")
- FDownloadDelegate OnSucc;
-
- //输出节点
- UPROPERTY(BlueprintAssignable, Category = "Vince | AsyncHttp")
- FDownloadDelegate OnFailed;
-
-
-
-
-
-
- };
- .cpp
-
-
- #include "AsyncHttpObject.h"
- #include "HttpModule.h"
- #include "Interfaces/IHttpResponse.h"
-
-
-
-
- //存储目录
- FString FileSavePath =TEXT("");
-
- //存储名字
- FString FileSaveName = TEXT("");
-
- UAsyncHttpObject* UAsyncHttpObject::AsyncHttpDownload(const FString& url, FString fileSavePath, FString fileSaveName)
- {
-
- UAsyncHttpObject* AsyncHttpObject = NewObject<UAsyncHttpObject>();
- AsyncHttpObject->DownloadRequestStart(url);
-
- FileSavePath = fileSavePath;
- FileSaveName = fileSaveName;
-
- return AsyncHttpObject;
- }
-
- //开始请求下载
- void UAsyncHttpObject::DownloadRequestStart(const FString& url)
- {
- TSharedRef<IHttpRequest, ESPMode::ThreadSafe> request = FHttpModule::Get().CreateRequest();
- request->SetVerb("GET");
- request->SetURL(url);
- request->OnProcessRequestComplete().BindUObject(this, &UAsyncHttpObject::DownloadRequestHandle); //请求回调
- request->OnRequestProgress().BindUObject(this, &UAsyncHttpObject::DownloadProgress);// 下载进度
- request->ProcessRequest();
-
-
-
- RemoveFromRoot(); // 手动GC
- }
-
- 请求回调
- void UAsyncHttpObject::DownloadRequestHandle(FHttpRequestPtr request, FHttpResponsePtr response, bool bWasSuccessful)
- {
- RemoveFromRoot();
-
-
- if (bWasSuccessful && response.IsValid() && EHttpResponseCodes::IsOk(response->GetResponseCode()))
- {
- if (response->GetContentLength() > 0) {
- TArray<uint8> EmptyData;
- OnProgressUpdate.Broadcast(response->GetContentLength(), response->GetContentLength(), EmptyData);
- }
-
- //FString FileSavePath = FPaths::ProjectDir() + "/download/gameplay.png";
-
-
-
- //下载返回类型设置
- FString sType, SLeft, SRight;
- sType = response->GetHeader(TEXT("Content-Type"));
- sType.Split("/",&SLeft,&SRight);
- FileSaveName.Append(FString::Printf(TEXT(".%s"), *SRight)) ;
-
- //组合 存储地址
- FileSavePath = FPaths::Combine(FileSavePath, FileSaveName);
-
- FString Path, Filename, Extension;
- FPaths::Split(FileSavePath, Path, Filename, Extension);
-
-
-
- IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
-
- if (!PlatformFile.DirectoryExists(*Path))
- {
- if (!PlatformFile.CreateDirectoryTree(*Path))
- {
- UE_LOG(LogTemp, Error, TEXT("Create Directory Failed!"));
- OnFailed.Broadcast();
- return;
- }
- }
-
- IFileHandle* FileHandle = PlatformFile.OpenWrite(*FileSavePath);
- if (FileHandle)
- {
- FileHandle->Write(response->GetContent().GetData(), response->GetContentLength());
- delete FileHandle;
- OnSucc.Broadcast();
- return;
- }
- else {
- UE_LOG(LogTemp, Error, TEXT("Save File Failed!"));
- OnFailed.Broadcast();
- return;
- }
-
- }
- UE_LOG(LogTemp, Error, TEXT("download File Failed!"));
- OnFailed.Broadcast();
- return;
- }
-
- // 下载进度
- void UAsyncHttpObject::DownloadProgress(FHttpRequestPtr request, int32 bytesSent, int32 bytesReceived)
- {
- if (request->GetResponse()->GetContentLength() > 0)
- {
- TArray<uint8> EmptyData;
- OnProgressUpdate.Broadcast(bytesReceived, request->GetResponse()->GetContentLength(), EmptyData);
- }
- }
也不知道怎么自动设置名字和后缀,可能有大佬知道怎么"微操".还望分享.这个下载进度的回调是正常的.我自己改的那个下载进度回调有问题.
在代码插件创建的Runtime Files Downloader - 虚幻引擎商城
在代码插件创建的Runtime Archiver - 虚幻引擎商城
*注意上面第一个变量必须指定文件.不然会失败
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。