ajax external url php file
Hello jQuery,
They need to upload huge files to ftp, because of the limitation of the plan,
I have partially written and copy code to make this work.
to use an external php fix this problem.
code snippet 1: jQuery/javascript:
- <script>
- $(document).ready(function(){
- $('#IDFormField_fileupload_0').change(function(){
- var file = this.files[0];
- name = file.name;
- size = file.size;
- type = file.type;
- //your validation
- });
- $('#IDFormField_fileupload_0').change(function(){
- var formData = new FormData($('form')[0]);
- $.ajax({
- url: 'http://www.externalurl.com/5.php', //server script to process data
- crossDomain: true,
- type: 'POST',
- xhr: function() { // custom xhr
- var myXhr = $.ajaxSettings.xhr();
- if(myXhr.upload){ // check if upload property exists
- myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
- }
- return myXhr;
- },
- //Ajax events
- success: function(data) {
- if (data.successMessage){//
- //set progress to 100%
- $("body").find('progress').attr({value:100,max:100});
- }
- if (data.errorMessage){
- console.log("error uploading file");
- }
- },
- data: formData,
- cache: false,
- contentType: false,
- processData: false
- });
- });
- });
- function progressHandlingFunction(e){
- if(e.lengthComputable){
- $('progress').attr({value:e.loaded,max:e.total});
- }
- }
- </script>
code snippet number 2 php code:
- <?php
- $ftp_server = "xxx";
- $ftp_username = "xxx";
- $ftp_password = "xxx!";
- //setup of connection
- $conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
- //login
- if(@ftp_login($conn_id, $ftp_username, $ftp_password))
- {
- echo "conectd as $ftp_username@$ftp_server\n";
- }
- else {
- echo "could not connect as $ftp_username\n";
- }
- if (!empty($_FILES)) {
- $file = $_FILES["IDFormField_fileupload_0"]["name"];
- $remote_file_path = "/".$file;
- ftp_put($conn_id, $remote_file_path, $_FILES["IDFormField_fileupload_0"]["tmp_name"], FTP_ASCII);
- }
- ftp_close($conn_id);
- echo "\n\nconnection closed";
- ?>
I have tested this outside of the his environment works perfectly.
when tested in his environment getting the following errors:
Failed to load resource: Origin http://www.website.com is not allowed by Access-Control-Allow-Origin. http://www.externalurl.com/5.php
XMLHttpRequest cannot load http://www.externalurl.com/5.php. Origin http://www.website.com is not allowed by Access-Control-Allow-Origin