VaRest icon indicating copy to clipboard operation
VaRest copied to clipboard

Upload image/file helper function

Open ibnufauzanalaula opened this issue 4 years ago • 16 comments

How to upload file (a csv file) along with other string data? For example, send data: name (string), id (string), and file (csv) to server

ibnufauzanalaula avatar Jan 30 '20 09:01 ibnufauzanalaula

It's not possible for now, but I'll take a look for upcoming release.

ufna avatar Jan 30 '20 10:01 ufna

That would be great. I need it too. :)

Minas314 avatar Feb 22 '20 19:02 Minas314

An ImageToByteArray function might do the job, as there is already a Base64Encode function for byte arrays in unreal. Base64 can be then uploaded as regular text and decoded server side. Not super efficient for frequent uploading but should do in most cases and would work with any byte arrays/files (not just images).

kasp1 avatar Feb 21 '21 11:02 kasp1

any progress for file uploading?

tuncayson avatar Oct 14 '21 17:10 tuncayson

This is how I upload files with VaRest: image

Minas314 avatar Oct 16 '21 20:10 Minas314

@Minas314 Can you please tell what is under the hood in the node "Read bytes from file"? I wanted to try your method of uploading files, but I'm stack on converting file to byte array.

@kasp1 the same question, can you please guide a little about suggested ImageToByteArray function?

My case is - I make request to some API and the response is just one image in .png format. How can I process this response for further usage of this .png file.

IlliaImm avatar Dec 28 '21 14:12 IlliaImm

@IlliaImm If you develop the API as well then you would server the image as a Base64 encoded string, which you would download using a VaRest request and then use the Base64 decode function to turn it into a byte array, which you could then convert to a texture

If the image is presented as the image (already a byte array) you could use this unreal engine core function to turn it into a texture: https://docs.unrealengine.com/4.26/en-US/BlueprintAPI/Class/AsyncTaskDownloadImage/DownloadImage/

kasp1 avatar Dec 28 '21 14:12 kasp1

@kasp1 Thank you for the response!

No, the API that I'm trying to communicate with is from some service provider, so I can't do anything with it's server side.

I tried that you recommended, but didn't succeeded in converting/decoding the response to smth that I can use. I attached screenshots to show what I get as a response and how I tried to decode it(i also tried without splitting the string - same result). And I'm not sure how I can use "DownloadImage" node, cause it requires simple URL. But my request consists not only of url, it also has a json object as a payload.

Hope you can help.

Screenshots

decode logmessage screenpng

IlliaImm avatar Dec 30 '21 10:12 IlliaImm

@IlliaImm Nope, looks like you are trying to Base64 decode the actual image, which won't work because the image is not base64 encoded but raw image data already.

If I understand your case correctly, you need something like this:

Screenshot_1

...or even better get the response content as a byte array right away if that's possible?

Tip: You should be able to use the response Content-Type header to figure out if the content is png image (if the value of this header equals "image/png".

kasp1 avatar Dec 30 '21 11:12 kasp1

@kasp1 Really appreciate your help. Sorry for bothering you many times.

Unfortunately, there is no option to get response content as a byte array. And unfortunately there is no unreal built-in function StringToByteArray. Also I checked if I can ask this API for some other types of data and the variants are only image/png (object) or image/jpeg (object) or image/tiff (object).

As you pointed in tip, I checked Content-Type and yes, it's image/png. So for now I didn't make it work for me. I will look for some other options and if I will find a working solution I'll post it here. I'm wondering if there is a possibility to just save response content as file in game directory and then just try to use this file? If you have some other suggestions I would really like to try it.

Thank you again.

IlliaImm avatar Dec 30 '21 13:12 IlliaImm

Maybe this function could be wrapped into a custom blueprint function and used in blueprints https://docs.unrealengine.com/4.26/en-US/API/Runtime/Core/Containers/StringToBytes/

kasp1 avatar Dec 30 '21 13:12 kasp1

I found solution that worked for me. I tried some different plugin that provides Byte Data of the response. And this Byte Data could be easily converted to a Texture by built-in function ImportBufferAsTexture2D.

So it would be really nice to have in VaRest plugin a possibility to get Byte Data of the response and also a possibility to save it as a file in project directory.

IlliaImm avatar Jan 03 '22 14:01 IlliaImm

image Hi is it possible to send form-data using VARest like use the request object and binary contents with files value. image

ssshammi avatar Apr 06 '22 22:04 ssshammi

Base64ToTexture2D

Hi! I make it work by using this way in UE5, Base64 to Texture2D

chase03 avatar Jul 21 '22 01:07 chase03

@chase03 Thanks i actually need to push to server, using multi-part form. And not read it. If you have been able to puch it let me know the data field is of type json and files.audioresponse is a binary file.

ssshammi avatar Jul 23 '22 23:07 ssshammi

This is how I upload files with VaRest: image So far I don't understand where you get that function from called "Read Bytes from File", VArest doesn't have it, nor is there a code in this thread to do it in c++

IsmaelPozzo avatar Apr 19 '23 22:04 IsmaelPozzo