1

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 i.e., 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>
2

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Maion,I know ajax is preferred for text data only but for progress bar I have to use ajax and just now I have posted the error log also its showing some request permission issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.