I want when a user uploads a file, this file should be sent to the server with FTP without pressing a button or submitting a form. I try to do that but it is not working.
Firstly, I created a function with time intervals, which checks whether the file input is empty or not. If it is not empty, it should send the file to the server.
var file = "/uploads/" + {{ api_id }}
this should be the address of the file on the server.
How can I do that or where is my mistake?
<input type="file" class="form-control-file" id="exampleFormControlFile1" name="bankStat1">
<script>
setInterval(displayHello, 1000);
function displayHello() {
var is_File = false
if (document.getElementById("exampleFormControlFile1").files.length >= 1){
is_File = true
var file = "/uploads/" + {{ api_id }}
var ftp = new FtpConnection("ftp://myftp/") ;
ftp.login("myuser", "mypass");
ftp.cd("uploads")
ftp.put(file,document.getElementById("exampleFormControlFile1").files[0]) ;
ftp.close() ;
file.close() ;
}
console.log(is_File)
}
</script>
I don't think there is a good solution for uploading files directly from client-side javascript to a server via FTP. The problem is that your FTP credentials need to be somewhere that the javascript can access them, but subsequently this means that that anyone with access to that javascript can simply use your FTP server as they please (a situation which is ripe for abuse.)
If you are simply trying to upload files to the server, HTML/HTTP provides file upload functionality on the client side and Django has the ability to do things with those files. If you have a server which is only accessible via FTP, I recommend that you upload the file to your django server and subsequently use a python FTP library to forward it from there. (I also recommend carefully validating the files that users have uploaded, as security problems abound from allowing client-supplied files to live on your server!)