Read file from server HTML 5

Hey everyone,

I have a packaged HTML 5 project sitting on my server.

I’m trying to pull a text file from the server and parse it into a string array using code from Rama’s BP library.

I have the same project packaged and working on Windows, but when I attempt to pull the file from the server it returns a null path. I’m assuming that has to do with the way FFileHelper is reading the file path, but I can’t seem to find another way around this.

I’ve tried out LE’s plugin series to properly request the files or read a JSON object rather than a text file. Unfortunately, all of the plugins cause build errors in blank projects all the way back to 4.13. All related to symbol definitions.

I’m still kinda sketchy when working in Unreal C++ so I was hoping to get some help pulling the file down. Everything I’ve done causes errors. Here’s the code I’m using to parse the text file for reference.

.h

#pragma once

#include "CoreMisc.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "StandardBlueprintFunctions.generated.h"


/**
 * 
 */
UCLASS()
class PROGEN_API UStandardBlueprintFunctions : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
/*Save String Array*/
public:
	
	/** Saves multiple Strings to filename of your choosing, with each string on its own line! Make sure include whichever file extension you want in the filename, ex: SelfNotes.txt . Make sure to include the entire file path in the save directory, ex: C:\MyGameDir\BPSavedTextFiles */
	UFUNCTION(BlueprintCallable, Category = "BPStandardFunctions")
		static bool FileIO__SaveStringArrayToFile(FString SaveDirectory, FString JoyfulFileName, TArray<FString> SaveText, bool AllowOverWriting = false);
/*Load String Array*/
public:
		/** Loads a text file from hard disk and parses it into a String array, where each entry in the string array is 1 line from the text file. Option to exclude lines that are only whitespace characters or '\n'. Returns the size of the final String Array that was created. Returns false if the file could be loaded from hard disk. */
		UFUNCTION(BlueprintPure, Category = "BPStandardFunctions")
		static bool LoadStringArrayFromFile(TArray<FString>& StringArray, int32& ArraySize, FString FullFilePath = "Enter Full File Path", bool ExcludeEmptyLines = false);

.cpp

#include "Progen.h"
#include "StandardBlueprintFunctions.h"

bool UStandardBlueprintFunctions::FileIO__SaveStringArrayToFile(FString SaveDirectory, FString JoyfulFileName, TArray<FString> SaveText, bool AllowOverWriting)
{
	//Dir Exists?
	if (!FPlatformFileManager::Get().GetPlatformFile().DirectoryExists(*SaveDirectory))
	{
		//create directory if it not exist
		FPlatformFileManager::Get().GetPlatformFile().CreateDirectory(*SaveDirectory);

		//still could not make directory?
		if (!FPlatformFileManager::Get().GetPlatformFile().DirectoryExists(*SaveDirectory))
		{
			//Could not make the specified directory
			return false;
			//~~~~~~~~~~~~~~~~~~~~~~
		}
	}

	//get complete file path
	SaveDirectory += "\\";
	SaveDirectory += JoyfulFileName;

	//No over-writing?
	if (!AllowOverWriting)
	{
		//Check if file exists already
		if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*SaveDirectory))
		{
			//no overwriting
			return false;
		}
	}

	FString FinalStr = "";
	for (FString& Each : SaveText)
	{
		FinalStr += Each;
		FinalStr += LINE_TERMINATOR;
	}



	return FFileHelper::SaveStringToFile(FinalStr, *SaveDirectory);

}
/** Load string array from file*/
bool UStandardBlueprintFunctions::LoadStringArrayFromFile(TArray<FString>& StringArray, int32& ArraySize, FString FullFilePath, bool ExcludeEmptyLines)
{
	ArraySize = 0;

	if (FullFilePath == "" || FullFilePath == " ") return false;

	//Empty any previous contents!
	StringArray.Empty();

	TArray<FString> FileArray;

	if (!FFileHelper::LoadANSITextFileToStrings(*FullFilePath, NULL, FileArray))
	{
		return false;
	}

	if (ExcludeEmptyLines)
	{
		for (const FString& Each : FileArray)
		{
			if (Each == "") continue;
			//~~~~~~~~~~~~~

			//check for any non whitespace
			bool FoundNonWhiteSpace = false;
			for (int32 v = 0; v < Each.Len(); v++)
			{
				if (Each[v] != ' ' && Each[v] != '\n')
				{
					FoundNonWhiteSpace = true;
					break;
				}
				//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			}

			if (FoundNonWhiteSpace)
			{
				StringArray.Add(Each);
			}
		}
	}
	else
	{
		StringArray.Append(FileArray);
	}

	ArraySize = StringArray.Num();
	return true;
}

Thanks for any help! Let me know if you need anything else.

Entro

I’m wondering if using IPlatformFile would be better than FFileHelper?

My server uses Unix based text file formatting. I figured this would just cause issues with symbols and formatting rather than invalidating the entire file, but could that cause the problem?

If so I could manually upload the files and run a script to change the format structure.

If anyone could offer up some insight on the build errors with the LE plugins that would be amazing as well. Reading the files as XML or JSON would be a lot easier for what I’m trying to do, but text files were the only thing I could think of…

My path formats were:
/home/…/public_html/wp-content/progen/progenhtml5.html &
www.sitename.com/wp-content/progen/progenhtml5.html

I’m using a GreenGeeks server too. I don’t think that would have anything to do with this, but it might help.

Thanks again!

Do you mean you have a proper webserver set up?

You would be way better off just using webrequests and possibly Json to read the data properly. I am not sure why you are doing file stuff at all. I am assuming the files are on a different machine, so reading a file with a path will most likely fail anyway.

Here is a nice and short tutorial on how to set up WebRequests with Unreal Engine. It pretty much covers what you need to requests the file as string and then parse it into a JsonObject.

Hey Denny,

Thanks for the answer! I’ve been working on a procedural language generator that reads text files for dynamic language creation.

I know it’s not really something Unreal is made for, but I wanted to see if it could be done and for Windows it can. Just more proof that Unreal is the best ;).

Everything is set up on the same server and everything is installed in the same directory so I don’t think it’s an issue with physical location, although I don’t know enough about complex server configuration to know that for sure.

My original plan was to read JsonObjects, but the plugins for HTTP requests and JsonObjects cause HTML5 builds to fail in every engine build back to 4.13 so these tutorials are clutch.

I might as well grow some stones and get out of the blueprint editor now anyway. =P

I’m going to take a crack at some stuff tomorrow and I’ll let you know how it goes.

Thanks again!

Entro