Pure JavaScript File Upload Example

  Рет қаралды 35,240

Cameron McKenzie

Cameron McKenzie

Күн бұрын

Пікірлер: 75
@kamentabakov5302
@kamentabakov5302 3 жыл бұрын
Can you give a link to all the code used?
@cameronmcnz
@cameronmcnz 3 жыл бұрын
Indeed. It's posted in the article that inspired this JavaScript File Upload Video over on TheServerSide: www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Ajax-JavaScript-file-upload-example
@kamentabakov5302
@kamentabakov5302 3 жыл бұрын
@@cameronmcnz Thank you
@steveacuin9241
@steveacuin9241 4 ай бұрын
I've been looking for a code like this. Better copy the codes from the link, it didn't work for me at first but found out that i had typo errors. Thanks for sharing!
@cameronmcnz
@cameronmcnz 4 ай бұрын
Happy to help! Happier that this still works 3 years after it was first published. 34K views? Might be worth updating this. Thanks for watching!
@jonnmostovoy2406
@jonnmostovoy2406 2 жыл бұрын
The alert on 4:49 is misleading. You should report success from ".then" of a promise.
@cameronmcnz
@cameronmcnz 2 жыл бұрын
Yeah, my use of the alert isn't semantically correct. I just wanted to give some quick visually feedback to the developer that things worked.
@peberdah
@peberdah 10 ай бұрын
Great, this is code example in Javascript and PHP that work, even pure Notepad. How to get filename, size of selected file, maybe an animated pure tag, again plus plus.
@Jon-jn7we
@Jon-jn7we 2 жыл бұрын
It doesn't work for me.. i am using the same server and all but it seems like the php file isn't being used at all because when i select the file and upload it it doesn't do anything. same code, same server...Any clue why is that?
@Evan-Lab
@Evan-Lab Жыл бұрын
Not working for me i searched solution since a week to upload a file and i don't find anything
@cameronmcnz
@cameronmcnz Жыл бұрын
What's the issue?
@charmilakondayagunta584
@charmilakondayagunta584 2 жыл бұрын
Hi friend, just going through this example... Pls correct me if I am wrong.. So if we want to upload a file using javascript Or jquery and using an ajax library, should there be definitely a backend code written to upload file to server.. Is it not possible only through front end???
@cameronmcnz
@cameronmcnz 2 жыл бұрын
Yes, 100% there must be some code running on the backend that expects the file to be uploaded and will then save the file on the server. In this example I believe I use PHP just because that's simple to set up on Apache. In this other video I use a Node.js library. People asked for JavaScript on both the frontend and backend so I put this together. But yes, a piece of HTML or JQuery on the client is not enough. Something on the server must be expecting the file. Here's the other upload video I did: kzbin.info/www/bejne/p6eYpX6bm5tgbqs
@charmilakondayagunta584
@charmilakondayagunta584 2 жыл бұрын
@@cameronmcnz o my god thanks a lot friend.. Iam a stupid, trying to upload file just by using front end ajax and wondering why it is not working.. Your answer really helped me to feel confident now..
@cameronmcnz
@cameronmcnz 2 жыл бұрын
@@charmilakondayagunta584 So glad i could help. Have fun learning HTML and JavaScript!
@RileyKalbOfficial
@RileyKalbOfficial Жыл бұрын
Getting Error 405 Method not allowed warnings. Any fix for this?
@richardwilkins5241
@richardwilkins5241 2 жыл бұрын
Great video!!
@cameronmcnz
@cameronmcnz 2 жыл бұрын
Glad I could help!
@emmaccode
@emmaccode 2 жыл бұрын
close, but no cigar for me. I am struggling to get this file reader event thing to work (rather than using forms :p) looks like I'm probably going to need to just use the forms.
@marianomv13
@marianomv13 3 жыл бұрын
Thanks! I needed to add a simple upload to a "backend-less website". This did the trick effortlessly.
@cameronmcnz
@cameronmcnz 3 жыл бұрын
Awesome! File uploads are tricky at the best of times. Super-glad you got it working.
@marianomv13
@marianomv13 3 жыл бұрын
@@cameronmcnz BTW, I still need to handle subfolder assignment and subfolder creation (if not yet created). Can you point me in the right direction? Maybe you've made a related video or know of a good tutorial to follow. Thanks again!
@craftingnerf2593
@craftingnerf2593 2 жыл бұрын
I tried to do this on my older raspberry pi but I get the error "500 (Internal Server Error)" its an older raspberry pi I saw on the chip 2011 so it could be its age either way is there some workaround to this?
@cameronmcnz
@cameronmcnz 2 жыл бұрын
You're a better man than me. I'm not sure if I could get it working on a newer Raspberry Pi!
@andrejkocsis4671
@andrejkocsis4671 Жыл бұрын
Fantastic!
@vincentprice3740
@vincentprice3740 3 жыл бұрын
thanks for sharing!
@cameronmcnz
@cameronmcnz 3 жыл бұрын
Sharing is caring. Thanks for the kind words!
@MimisK88
@MimisK88 3 жыл бұрын
It works. Thanks! Can you give an example of how to handle multiple files?
@الإسلامدينالحق-خ5ت
@الإسلامدينالحق-خ5ت 2 жыл бұрын
as a beginning to know the purpose of your existence in this life..👇 .My friends, search for your life purpose, why are we here?? I advise you to watch this series kzbin.info/aero/PLPqH38Ki1fy3EB-8xmShVqpbQw99Do2B-
@Bantinck
@Bantinck 2 жыл бұрын
Thanks a lot man. You saved my time.
@relaxationology
@relaxationology 2 жыл бұрын
Can that upload music file and video file? If not, how can I do it.
@cameronmcnz
@cameronmcnz 2 жыл бұрын
Yup! Should work as long as you don't hit any size limitations. I think for really big files you can do multipart uploads.
@divinechukwu505
@divinechukwu505 2 жыл бұрын
Thank You For This
@cameronmcnz
@cameronmcnz 2 жыл бұрын
Glad I could help!
@aldepaixao
@aldepaixao Жыл бұрын
preciso mesmo é enviar múltiplos arquivos. você tem esse vídeo aqui?
@AshwinSKumar
@AshwinSKumar Жыл бұрын
You should make a video on how to automatically upload multiple files, that are same filetype to the cloud,
@cameronmcnz
@cameronmcnz Жыл бұрын
Imma do that. Been meaning to do that for a while!
@AshwinSKumar
@AshwinSKumar Жыл бұрын
@@cameronmcnz Cool Awesome
@alaaeddinp
@alaaeddinp Жыл бұрын
I am really curious why you're using notepad ^_^?
@cameronmcnz
@cameronmcnz Жыл бұрын
It's all I could afford! Just trying to keep the example as simple as possible.
@tochukwuokeke5348
@tochukwuokeke5348 3 жыл бұрын
I tried same code and it didn’t upload it just alerts file has been uploaded successfully, but nothing is in upload folder and there are no errors. Any advise. I am testing with localhost xampp
@marianomv13
@marianomv13 3 жыл бұрын
I had a similar error, but it was my mistake. I had the PHP file in a "PHP" folder referencing a "uploads" folder which actually was "../uploads" due to my folder structure. Hope that helps.
@liloolilful
@liloolilful 2 жыл бұрын
It does NOT work if you use BLOB or new File .... via javascript :(
@timdrchslr
@timdrchslr 3 жыл бұрын
Hello, Is there a maximum file size limitation? I googled a lot but nothing helped... smaller files like pictures are working.
@الإسلامدينالحق-خ5ت
@الإسلامدينالحق-خ5ت 2 жыл бұрын
as a beginning to know the purpose of your existence in this life..👇 .My friends, search for your life purpose, why are we here?? I advise you to watch this series kzbin.info/aero/PLPqH38Ki1fy3EB-8xmShVqpbQw99Do2B-
@張紘齊-s4k
@張紘齊-s4k Жыл бұрын
I tried the same code but get Uncaught TypeError TypeError: Failed to fetch
@AshwinSKumar
@AshwinSKumar Жыл бұрын
You should make a video on how to edit .php script file with Google Web Designer.
@yacine5059
@yacine5059 3 жыл бұрын
it works but what about multiple files ??
@cameronmcnz
@cameronmcnz 3 жыл бұрын
That'll be the next video. But basically, use the mutli-file upload HTML element and handle a list of elements on the server.
@zakhariihusar6975
@zakhariihusar6975 Жыл бұрын
Hi. I've read that it's safe to keep the php scripts in apache root folder (eg var/www/html)as long as php libpache mod is enabled. But could you please tell me how to access php api on the same server? Still via http requests?
@zakhariihusar6975
@zakhariihusar6975 Жыл бұрын
I mean to access from JavaScript (i wanna use React JS on front-end and php api on back end)
@raf4killer299
@raf4killer299 2 жыл бұрын
thanks!
@cameronmcnz
@cameronmcnz 2 жыл бұрын
Glad I could help! I've got another video where the entire backend is JavaScript and Node. Sometimes people who want the JavaScript upload steps want everything in JavaScript, even the backend.
@stephanieharvill4157
@stephanieharvill4157 2 жыл бұрын
Got it working just fine Thanks Cameron... But the file size that will upload seems to max out at under 2Meg... Anyone able to upload a file bigger than 2Meg???
@stephanieharvill4157
@stephanieharvill4157 2 жыл бұрын
btw Apache 2.4
@cameronmcnz
@cameronmcnz 2 жыл бұрын
@@stephanieharvill4157 I'm not totally sure. 2 meg doesn't see that huge. Maybe an Apache setting? I know for really big files you're supposed to do a 'multipart upload'.
@danielwestlund9208
@danielwestlund9208 3 жыл бұрын
Title and description say pure javascript. I almost stopped watching when I saw the apache server, but stopped watching when I saw upload.php.
@cameronmcnz
@cameronmcnz 3 жыл бұрын
This is part of an HTML, JQuery and DOJO file upload set of articles. So the title fits in with the series and makes total sense in that context. The description also states that it's PHP on the backend on an Apache server. So there's no intention to mislead to get clicks. But, it seems like Google/KZbin searches have promoted this to people looking for Node or JS on the backend, so there's a mismatch in searcher intent. I apologize for that but it's out of my control. I'd like to put in the description 'this is not for node.js or JS on the backend,' but I have a feeling adding those terms to the description will have Google/KZbin incorrectly recommending it even more. I do apologize for the inconvenience.
@emmaccode
@emmaccode 2 жыл бұрын
you didn't really think you'd be able to write a file client-side, did you??
@maxpositivity3979
@maxpositivity3979 3 жыл бұрын
I tried with the same code and folder structure but did not worked.
@cameronmcnz
@cameronmcnz 3 жыл бұрын
Let's fix it! What server were you using? Apache server? Tomcat? Any errors?
@devanshusingh225
@devanshusingh225 3 жыл бұрын
Same issue
@cameronmcnz
@cameronmcnz 3 жыл бұрын
@@devanshusingh225 Apache server on the backend?
@TITAN-sv5eg
@TITAN-sv5eg Жыл бұрын
nice
@AddittionsLearning
@AddittionsLearning Жыл бұрын
this aint pure js ur using php🤧
@cameronmcnz
@cameronmcnz Жыл бұрын
This is all client-side JS. For client and serverside JavaScript, here's the article and video I did on it. Enjoy! www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/JavaScript-Nodejs-File-Upload-Example-Ajax
@tanimsk
@tanimsk 3 жыл бұрын
thanks!!
@cameronmcnz
@cameronmcnz 3 жыл бұрын
So glad I could help. Have fun with JavaScript!
@tanimsk
@tanimsk 3 жыл бұрын
@@cameronmcnz will do, thanks again!
@الإسلامدينالحق-خ5ت
@الإسلامدينالحق-خ5ت 2 жыл бұрын
as a beginning to know the purpose of your existence in this life..👇 .My friends, search for your life purpose, why are we here?? I advise you to watch this series kzbin.info/aero/PLPqH38Ki1fy3EB-8xmShVqpbQw99Do2B-
@beltsasarazpeitia5503
@beltsasarazpeitia5503 2 жыл бұрын
Excuseme i want to upload an entire folder
@الإسلامدينالحق-خ5ت
@الإسلامدينالحق-خ5ت 2 жыл бұрын
as a beginning to know the purpose of your existence in this life..👇 .My friends, search for your life purpose, why are we here?? I advise you to watch this series kzbin.info/aero/PLPqH38Ki1fy3EB-8xmShVqpbQw99Do2B-
@francogutierreztamayo475
@francogutierreztamayo475 2 жыл бұрын
No sirve, no sube nada
@raf4killer299
@raf4killer299 2 жыл бұрын
*htm:* Enviar *js:* async function EnviarCertificados(){ let arquivo = document.getElementById('inputcertificados').files //pega a imagem let formData = new FormData(); formData.append("inputcertificados", arquivo[0]); //id e o nome do arquivo await fetch('/class/uploadCurriculo.php', {method: "POST", //faz o envio pelo método POST body: formData} ).then( console.log("Enviado com sucesso") //caso seja enviado ).catch((erro)=>{ console.log(erro) //caso aconteça algum erro }); } *php:* $arquivosPermitidos = array("png", "jpg", "pdf", "gif", "doc", "docx"); $extensao = pathinfo($_FILES['inputcertificados']['name'], PATHINFO_EXTENSION); if(in_array($extensao, $arquivosPermitidos)): //verifica se existe a extensão no array $pasta = "../assets/uploadCurriculo/"; //pasta onde irá salvar $temporario = $_FILES['inputcertificados']['tmp_name']; $novoNome = uniqid().".$extensao"; if(move_uploaded_file($temporario, $pasta.$novoNome)): $mensagem = "Upload feito com Sucesso!"; else: $mensagem = "Erro, não foi possivel fazer o upload"; endif; else: $mensagem = "Formato inválido"; endif; echo $mensagem;
@cameronmckenzie7188
@cameronmckenzie7188 4 жыл бұрын
If you want a text based step-by-step walk through of this JavaScript file upload tutorial I did a full write up on TheServerSide: www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Ajax-JavaScript-file-upload-example
JavaScript and Ajax File Upload Example
7:35
Cameron McKenzie
Рет қаралды 3,5 М.
File Upload using Fetch API - JavaScript Tutorial
9:02
OpenJavaScript
Рет қаралды 27 М.
Seja Gentil com os Pequenos Animais 😿
00:20
Los Wagners
Рет қаралды 39 МЛН
Un coup venu de l’espace 😂😂😂
00:19
Nicocapone
Рет қаралды 12 МЛН
А что бы ты сделал? @LimbLossBoss
00:17
История одного вокалиста
Рет қаралды 10 МЛН
HTML File Upload Tutorial with an HTML5 Input Form
7:00
Cameron McKenzie
Рет қаралды 27 М.
PHP on the frontend! No more Javascript!
14:47
Aaron Francis
Рет қаралды 124 М.
JSON and AJAX Tutorial: With Real Examples
40:45
LearnWebCode
Рет қаралды 1,8 МЛН
I Found a BETTER Way to Do File Uploads
8:30
James Q Quick
Рет қаралды 15 М.
jQuery File Upload Example
8:10
Cameron McKenzie
Рет қаралды 14 М.
Seja Gentil com os Pequenos Animais 😿
00:20
Los Wagners
Рет қаралды 39 МЛН