How to Upload files easily to a server using Javas

2019-09-11 04:00发布

问题:

I am absolutely new to programming. I have chosen Django to start, and I am learning through tutorials and pages like this. What I am trying to do is this: I have a .txt file in my root folder, and when I run a particular web page, I take information from that file and display some stuff. The problem is that I need that the users can upload a .txt from their computers, and that this file replaces the file in my root folder. So, that it is called for example data.txt and stored, and that the page refreshes (like F5, for now) so that the information shown is taken from that new file. Can anybody tell me how to do this, explained step by step 'for Dummies'? If possible, I would like to do it just in JavaScript (not Python). I don't need multiple uploads, and it is not necessary that the upload involves AJAX or anything like that, despite I know that it is nicer. Thank you very much!

回答1:

first, javascript cannot upload a file from the client's computer, otherwise I would be able to send you to a website that would steal all of the files on your computer.

second, all you need to upload a file is an html form, where the user can select a file, then submit the form, thus uploading it to your server. a server side script is then needed to move the uploaded file to wherever you want it.

third, you should realllly be careful about allowing uploads to your server, as this can be a gaping vector for malware to be installed on your server if not done correctly.

fourth, since you are using django as your server side language, you will probably just want to google "uploading files using django". Here is the django documentation on how to do file uploads:

https://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs



回答2:

It seems like you wouldn't need a javascript solution to do that and using basic Django could work. However if you keep interested in manipulating files with Javascript there's an explanation on how to do that at:

How to preprocess a local CSV file before uploading to a server?