简体   繁体   中英

File upload to PHP server using ajax is not working

I am using the below code to upload file to the online test server( http://posttestserver.com/post.php ) and it is getting successfully uploaded and the code is also working fine as required but the issue is I have to upload the file to an internal PHP server ie, https://xxxx.xxxx.xxxxx.com/sites/default/files/FileUpload/upload_file.php which is not working for below code while if I remove the ajax,progress bar and script part and run the code only with form tag it can upload the file.

Please help me I cant able to track the issue and I dont have knowledge in PHP.

Also find below the Error log in comment.

<!DOCTYPE html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<style>
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }
#progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
#bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
#percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
</head>
<body>
<h1>Ajax File Upload Demo</h1>
<form id="myForm" action="http://posttestserver.com/post.php" method="post" enctype="multipart/form-data">
<input type="file" size="60" name="file">
<input type="submit" value="Ajax File Upload">
</form>
<div id="progress">
<div id="bar"></div>
<div id="percent">0%</div >
</div>
<br/>
<div id="message"></div>
<script>
$(document).ready(function()
{
var options = { 
beforeSend: function() 
{
$("#progress").show();
$("#bar").width('0%');
$("#message").html("");
$("#percent").html("0%");
},
uploadProgress: function(event, position, total, percentComplete) 
{
$("#bar").width(percentComplete+'%');
$("#percent").html(percentComplete+'%');
},
success: function() 
{
$("#bar").width('100%');
$("#percent").html('100%');
},
complete: function(response) 
{
    $("#message").html("<font color='green'>"+response.responseText+"</font>");
},
error: function()
{
    $("#message").html("<font color='red'> ERROR: unable to upload files</font>");
}     
}; 
$("#myForm").ajaxForm(options);
});
</script>
</body>
</html>

Saikat, using ajax to upload file is a pain in the a..

check this out for your referrence

http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM