# How do i send/receive files with pure JS?

ok so I was making a cloud thing but I was confused "how do I upload and download files with JS?" I was looking on google, but none of them worked in the snap editor. can anyone help me?

when they say "upload files" I think they mean, "upload them to an external server". Also, you could just link them to the File import library instead of your custom blocks project.

yes i meant that
is there any external servers that i can send stuff to?

make your own with replit or something

https://transfer.sh, but uploads only stay up for 14 days

but how do you do it with javascript?

to get files you can use fetch

fetch("https://www.example.com/")
.then(response => response.json())
.then(data => console.log(data));


to send files you can probably send a POST or PUT request using an XMLHttpRequest
i say "probably" because i think it is up to the server to decide what happens on a POST or PUT request

var req = new XMLHttpRequest();

req.open("POST", "https://www.example.com/");
req.send(data);

You can also send POST or PUT requests with fetch

let i = document.createElement("input")
i.type = "file"
// when clicked
i.click()
//etc


You gotta make a Blob object and attach it onto a <a> tag
Blob - Web APIs | MDN for more details

I didn't either. I just searched on the forum, "file import" and in the results was "File import library".

Take a look:

var lnk = document.createElement('a');
lnk.href = (URL||webkitURL).createObjectURL(new Blob([contents])); // assuming contents is a var with the contents
lnk.click()

with the block in the

with all website except snap.berkeley.edu it just reports nothing

im trying to use it in snap
edit: i will try again

im trying to use it in snap

test
wait button tag does not work?

You need to define a variable or function parameter called data and assign to it the data you want to send.

Same thing (

)

Only a subset of HTML can be used on the forum (i.e. iframe for Snap! projects and text-formatting ones like p, h1-h6, b, i, strike, em, etc. [maybe more?])