I have a model which I have uploaded to an AnyLogic private cloud. I have been successfully able to run it, pass inputs and retrieve outputs of my model with any issue. I am running this via a locally hosted web app
I have been trying to set up my model so the user can upload a .json file containing string inputs needed to run the model. Unfortunately I cannot get this to work. Currently I am testing with a json file containing the following:
{"testValue" : 123}
I have an html input element which is able to upload and pass a file to my javascript code:
<h2>Upload file</h2>
<div className="flex">
<input type="file" id="fileInput" onChange={handleFileUpload} style={{ position: 'relative', zIndex: 100 }}></input>
</div>
function handleFileUpload(event:ChangeEvent<HTMLInputElement>) {
const target = event.target as HTMLInputElement //target is the input element
if(target && target.files){
if(sim){
//function to upload file and then run an animated model
runAnimation(sim.anylogicModelId, runButtonId, runMonteCarloId, animationContainerId, target)
}
}
}
Code in runAnimation:
cloudClient.uploadFile(input)
.then((hash) => {
//console.log("set file name ", file.name)
inputFile = {fileName: file.name, resourceName: file.name, hash};
//console.log("inputfile ", inputFile)
});
the upload file method then runs the code to get the hash of the file I am uploading and then passes input.value to the api request to upload the file. This is where I am confused:
- input.value just seems to return the file path of the input file. For me this gets C:\fakepath\smallTest.json because browsers automatically block access to the file path
- Nowhere in the code of the example or cloud client can I see where it actually passes the content of the file to the cloud. Just the hash and name. The python example looks like it instead passes the streamed content of the file
- The example seems to pass the input to upload file in one place but then the file itself to upload file in another. The cloud client class only has one input file method and its code uses methods like .files[0] which only work on the input reader
I have tried modifying the cloud client to pass in different objects to the upload-file request to see if I could get any different results
Here are the following things I have tried:
- Passing the file itself as an object
- Passing the arraybuffer content of the file using the cloud clients readfile method, also tried parsing this to a byte array
- Passing the file name rather than the file path (online it says both are possible outputs of input.value)
- Temporarily hardcoding the path to the file in my documents and passing that to bypass the issue of the path being hidden when calling input.valye
- Uploading a .txt file instead to check if the issue is with the .json file type
Not only do none of these work but the api request always returns an identical hash code for the uploaded file. It is always returned as a successful request and I am able to track it as far as the logs for the front end on my cloud. I have ran out of ideas on things to try at this point
I have not found any online examples of anyone who has used the javascript api for uploading files or any other questions asked about it. The only information is what is on the api page and in the cloudclient.js file. Has anyone here used this feature and are able to give me any more insight into how it works?