question

Joe Uphaus avatar image
Joe Uphaus asked

Javascript SDK Upload entity file help

This is likely just my ignorance of javascript but wanted to see if someone could help with the correct way to upload a file to playfab once the upload url is received. I am using javascript SDK in Construct 3.

The below will successfully use the InitiateFileUploads api to receive a response with an upload url. I am already logged in and getting the appropriate entityId / type and passing it in. I would like now to do a put using that url to upload sample data which I call DataTest below into an entity file on playfab.

What is the proper way to do this in javascript? Does it need to be encoded? I am attempting to use fetch in an async function but am I simply approaching it incorrectly? Any help is appreciated!

Also, I was able to get their setObjects api working correctly which could also be used to store small sets of data as demo'd below but I am interested in storing larger amounts of information which requires the use of an entity file.

The closest documentation on playfab is below under the entity files section:

api.playfab.com/docs/tutorials/entities/getting-started-entities

//InitiateFileUploads
//https://api.playfab.com/documentation/Data/method/InitiateFileUploads
function PF_InitiateFileUploads(f_EntityId,f_EntityType,f_EntityTypeString,mapData){

	var request = {
		"FileNames": [
			"mapData"
		],
		"Entity": {
			"Id": f_EntityId,
			"Type": f_EntityType,
			"TypeString": f_EntityTypeString
		}
	}

	PlayFabDataSDK.InitiateFileUploads(request,onInitiateFileUploadsResponse);
}
function onInitiateFileUploadsResponse(response, error) {
	//console.log(gMapData);
	var uploadURL;
	var DataTest={
		posX: 45,
		posY: 23
	};

	if (response !== null) {
		console.log("InitiateFileUploads response received")
		console.log("InitiateFileUploads response: " + JSON.stringify(response));

		uploadURL = response.data.UploadDetails.UploadUrl;

		loadFile(uploadURL,DataTest)

	} else if (error !== null) {
		console.log("InitiateFileUploads error received")
		console.log("InitiateFileUploads error: " + JSON.stringify(error));

	}
}

async function loadFile(uploadURL,data){
	var payload = data;
	var otherParam={
		headers:{
			"content-type":"application/json; charset=UTF-8"
		},
		body:payload,
		method:"PUT"
	};
	const response = await fetch(uploadURL,otherParam);
	const jsonData = await response.json(); 

}

sdksentities
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

You can refer to the following JavaScript example of the PUT request.

...
function onInitiateFileUploadsResponse(response, error) 
{
... 
var customString = "Hello";
var url = response.data.UploadDetails.UploadUrl;
PutRequest(url,customString);
...
}

function PutRequest(url,stream){
    const request = new XMLHttpRequest(); 
    request.onreadystatechange = function () { 
        if (request.readyState === 4) { 

            if (request.status === 200) {
                //Succeeded
                console.log("Succeeded");
            } else {
                //Failed
                console.log("Failed");
            }
        } else {
            //Waiting
            console.log("Waiting");
        }
    }
    
    request.open('PUT', url);
    request.send(stream);
}
...

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.