I need help: JS & files

Let me know if this is the wrong file
My Project11-1-2020_6-18-48PM.wick (5.7 KB)

What I’m working on is an upload button, & the file above is the farthest I really got…
Maybe someone could help? I’ll be guessing since the mods made the text asset, they might know a way to make this work.

What type of file am I trying to make it open?
I’m simply trying to make it open something, and I don’t really care what format as long as it actually opens something.

Thank you 4 reading : )

1 Like

What you’ve got so far seems really impressive to me. Would you be adding the assets uploaded with that feature to the asset library?

Honestly, I’m not really sure.

I already had done a whole research of this today, & all I found was that I needed to add only 1 line to make that project end up like that, so my knowledge is still little of how this works

Oh yeah I kinda forgot jquery existed ( $(input).click(); ) since I always use native JavaScript.

Ok so what you want to use is

input.onchange = function() {
  console.log(this.files) //use this.files (or input.files)
}

Then to read the files you use:

var reader = new FileReader();

reader.onload = function() {
  console.log(this.response); //or this.responseText
}

reader.readAsText(file);

There are different reader.readAs functions. If you want a text file, use reader.readAsText. If you want an media file, use reader.readAsDataUrl. And if you want any other file format (like MIDI or a custom file format) you use reader.readAsArrayBuffer or reader.readAsBinaryString.

2 Likes

Also, I think you might want a method to make the user download a file. Here’s how to do it:

var blob = new Blob([fileContents], {"type": mimeType});
var url = URL.createObjectURL(blob);

var link = document.createElement("a");
a.download = fileName; //including extension
a.href = url;
a.click();

If you want to export a text file, mimeType should be text/plain. If you want to export a javascript file, mimeType should be text/javascript. If you want to export a JSON, use application/json. If you want to export a custom file format you can use application/octet-stream. There are more MIME types you can search them up.

2 Likes

Wow, I think this is everything that I need to know!
I’ll try to play around with it and see how it works out : )

@pumpkinhead should really become a moderator, he just has a lot of knowledge