成果

功能:点击一个按钮可以远程下载文件 并看得到进度条

先上代码
.h文件

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "Interfaces/IHttpRequest.h"
#include "RuntimeFilesDownloaderLibrary.generated.h"/*** */
UENUM(BlueprintType, Category = "Runtime Files Downloader")
enum DownloadResult
{SuccessDownloading UMETA(DisplayName = "Success"),DownloadFailed UMETA(DisplayName = "Download failed"),SaveFailed UMETA(DisplayName = "Save failed"),DirectoryCreationFailed UMETA(DisplayName = "Directory creation failed")
};
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FOnFilesDownloaderProgress, const int32, BytesSent, const int32, BytesReceived,const int32, ContentLength);/***/
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnFilesDownloaderResult, TEnumAsByte < DownloadResult >, Result);UCLASS()
class DOWNLOAD_API URuntimeFilesDownloaderLibrary : public UObject
{GENERATED_BODY()
public:UPROPERTY(BlueprintAssignable, Category = "Runtime Files Downloader")FOnFilesDownloaderProgress OnProgress;/***/UPROPERTY(BlueprintAssignable, Category = "Runtime Files Downloader")FOnFilesDownloaderResult OnResult;/***/UPROPERTY(BlueprintReadOnly, Category = "Runtime Files Downloader")FString FileURL;/***/UPROPERTY(BlueprintReadOnly, Category = "Runtime Files Downloader")FString FileSavePath;/***/UFUNCTION(BlueprintCallable, Category = "Runtime Files Downloader")static URuntimeFilesDownloaderLibrary* CreateDownloader();/***/UFUNCTION(BlueprintCallable, Category = "Runtime Files Downloader")bool DownloadFile(const FString& URL, const FString& SavePath, float TimeOut = 5);
private:void OnProgress_Internal(FHttpRequestPtr Request, int32 BytesSent, int32 BytesReceived);/****/void OnReady_Internal(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
};

.cpp文件

// Fill out your copyright notice in the Description page of Project Settings.#include "RuntimeFilesDownloaderLibrary.h"
#include "HttpModule.h"
#include "Interfaces/IHttpRequest.h"
#include "Interfaces/IHttpResponse.h"URuntimeFilesDownloaderLibrary* URuntimeFilesDownloaderLibrary::CreateDownloader()
{URuntimeFilesDownloaderLibrary* Downloader = NewObject<URuntimeFilesDownloaderLibrary>();Downloader->AddToRoot();return Downloader;
}bool URuntimeFilesDownloaderLibrary::DownloadFile(const FString& URL, const FString& SavePath, float TimeOut)
{if (URL.IsEmpty() || SavePath.IsEmpty() || TimeOut <= 0){return false;}FileURL = URL;FileSavePath = SavePath;//TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();TSharedPtr<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();HttpRequest->SetVerb("GET");HttpRequest->SetURL(FileURL);//HttpRequest->SetTimeout(TimeOut);HttpRequest->OnProcessRequestComplete().BindUObject(this, &URuntimeFilesDownloaderLibrary::OnReady_Internal);HttpRequest->OnRequestProgress().BindUObject(this, &URuntimeFilesDownloaderLibrary::OnProgress_Internal);// Process the requestHttpRequest->ProcessRequest();return true;
}void URuntimeFilesDownloaderLibrary::OnProgress_Internal(FHttpRequestPtr Request, int32 BytesSent, int32 BytesReceived)
{const FHttpResponsePtr Response = Request->GetResponse();if (Response.IsValid()){const int32 FullSize = Response->GetContentLength();OnProgress.Broadcast(BytesSent, BytesReceived, FullSize);}
}void URuntimeFilesDownloaderLibrary::OnReady_Internal(FHttpRequestPtr Request, FHttpResponsePtr Response,bool bWasSuccessful)
{RemoveFromRoot();Request->OnProcessRequestComplete().Unbind();if (Response.IsValid() && EHttpResponseCodes::IsOk(Response->GetResponseCode()) && bWasSuccessful){//IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();// FString Path, Filename, Extension;FPaths::Split(FileSavePath, Path, Filename, Extension);if (!PlatformFile.DirectoryExists(*Path)){if (!PlatformFile.CreateDirectoryTree(*Path)){OnResult.Broadcast(DirectoryCreationFailed);return;}}// IFileHandle* FileHandle = PlatformFile.OpenWrite(*FileSavePath);if (FileHandle){// FileHandle->Write(Response->GetContent().GetData(), Response->GetContentLength());// delete FileHandle;OnResult.Broadcast(SuccessDownloading);}else{OnResult.Broadcast(SaveFailed);}}else{OnResult.Broadcast(DownloadFailed);}
}

代码参考的知乎大佬 注意UE4.27 http里

 TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();注意这段代码已经不适用了 根据UE官方源码4.27已经修改成TSharedPtr<IHttpRequest, ESPMode::ThreadSafe> Request = FHttpModule::Get().CreateRequest();

UE4C++ Http下载文件相关推荐

  1. 用python下载文件的若干种方法汇总

    压缩文件可以直接放到下载器里面下载的 you-get 连接 下载任意文件 重点 用python下载文件的若干种方法汇总 写文章 用python下载文件的若干种方法汇总 zhangqibot发表于Met ...

  2. 初级版python登录验证,上传下载文件加MD5文件校验

    服务器端程序 import socket import json import struct import hashlib import osdef md5_code(usr, pwd):ret = ...

  3. linux快捷上传下载文件

    借助securtCRT,使用linux命令sz可以很方便的将服务器上的文件下载到本地,使用rz命令则是把本地文件上传到服务器 其中,对于sz和rz的理解与记忆我用了如下的方法(因为很多时候容易搞混): ...

  4. 基于php下载文件的详解

    基于php下载文件的详解 本篇文章是对php下载文件进行了详细的分析介绍,需要的朋友参考下 php下载文件,比如txt文件. 出现的效果就是,弹出浏览器自带的下载框,出现另存为操作.有时候会出现内存溢 ...

  5. java上传加密_Java上传下载文件并实现加密解密

    使用 Jersey 服务器实现上传,使用 HTTP 请求实现下载 引入依赖 在 pom.xml 中添加 Jersey 相关依赖 com.sun.jersey jersey-client 1.18.1 ...

  6. sftp shell 批量上传文件_Shell自动上传下载文件到SFTP服务器

    1.说明 本文提供一个Shell脚本, 可以自动连接到SFTP服务器, 然后上传或者下载指定的文件, 进而可以使用Linux的corntab命令, 定时执行脚本上传下载文件, 实现文件的同步或者备份功 ...

  7. php 当前页面下载文件,php实现当前页面点击下载文件的简单方法

    php实现当前页面点击下载文件的简单方法 发布于 2017-08-02 17:44:21 | 80 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: Hypertext ...

  8. ASP.NET 下载文件方式

    protected void Button1_Click(object sender, EventArgs e){/*微软为Response对象提供了一个新的方法TransmitFile来解决使用Re ...

  9. java urlconn 下载慢_使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法...

    import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.RandomAc ...

  10. 批量下载文件,循环中文件流没有关闭导致每次下载会累加之前的下载文件

    /*** 压缩** @param files 多文件* @param zipFilePath 目标压缩文件路径* @throws IOException*/ public static void zi ...

最新文章

  1. samba srver on centos-7
  2. android挂载usb设备,android usb挂载分析---MountService启动
  3. 在阿里做科研是一种什么感受?
  4. hibernate映射一对多双向关联关系实例
  5. 图像处理(三)图像分割(1)Random Walks分割
  6. 分布式光伏发电并网无功补偿问题
  7. 线性规划之单纯形法(2)
  8. linux之gdb基本调试命令和使用总结
  9. 操作 实例 / dom
  10. js unescape 对应php的函数,php实现Javascript的escape和unescape函数
  11. 改进初学者的PID-手自动切换
  12. 变位齿轮重合度计算公式_渐开线圆柱齿轮传动的重合度计算.pdf
  13. XMLHttpRequest 学习(二)——封装一个ajax
  14. Cannot use a leading .. to exit above the top directory
  15. Git 及 GitHub实用教程
  16. 海思Hi3798硬件设计,Hi3798 datasheet(2)参考资料
  17. 完美解决IE11和补丁安装不上方案
  18. 像电影里黑客高手一样写代码
  19. java.lang.exceptionininitializererror_java.lang.ExceptionInInitializerError异常的解决方法
  20. 网联V2X测试解决方案

热门文章

  1. 音频格式之AAC(高级音频编码技术)
  2. 最全最详细的小白快速上手Modelsim教程
  3. 1.使用WPE工具分析游戏网络封包
  4. HG255D电信原版刷机实战整理,既HG255D刷uboot、Openwrt
  5. 智能硬件产品开发分享
  6. 十、Net6 Core Api发布到IIS
  7. MapX bug 和设计缺陷
  8. Java学习之json篇——json介绍
  9. linux 端口映射
  10. java环境变量如何配置?