问题
I am trying a simple file upload of a tar file using jquery. But I get an Invalid archive format from my server. Any help would be appreciated.
PS: I cannot change the server code. It comes from another team.
Edit 1: Response from chrome: My HTML:
<!DOCTYPE html>
<html>
<body>
<form method="POST" enctype="multipart/form-data" id="testFileUpload">
<input type="file" name="selectedFile"/>
<input type="submit" value="Submit" id="btnSubmit"/>
</form>
</body>
</html>
My JS:
$(document).ready(function () {
$("#btnSubmit").click(function (event) {
event.preventDefault();
var form = $('#testFileUpload')[0];
var data = new FormData(form);
$.ajax({
type: "POST",
enctype: 'multipart/form-data',
url: g_url_version+'/hosting/apps',
data: data,
processData: false,
contentType: false,
timeout: 600000,
beforeSend: function (xhr) {
xhr.setRequestHeader('X-Token-Id', getCookieToken());
xhr.setRequestHeader('X-Connector-Id', 'TestSeed');
},
success: function (data) {
console.log("SUCCESS : ", data);
},
error: function (e) {
console.log("ERROR : ", e);
}
});
});
});
I can upload the file using postman via Binary as shown below but not via formData.
回答1:
Bind the submit event handler rather than click on the button as you need to prevent the default form submission and send manual ajax call
$("#btnSubmit").click(function (event) {
to
$("#testFileUpload").on('submit', function(event) {
and simply use this
to get the form object inside it
var form = this;
copy the code below it should upload the file correctly if you have a simple php
file with
print_r($_POST);
print_r($_FILES);
it should show the below in the console
SUCCESS : Array
(
[userid] => oemr@omer.com
[filelabel] =>
)
Array
(
[file] => Array
(
[name] => IMG_20160521_092941676.jpg
[type] => image/jpeg
[tmp_name] => F:\xampp\tmp\phpD881.tmp
[error] => 0
[size] => 4867779
)
)
$(document).ready(function() {
$("#testFileUpload").on('submit', function(event) {
event.preventDefault();
var form = this;
var data = new FormData(form);
$.ajax({
type: "POST",
enctype: 'multipart/form-data',
url: g_url_version + '/hosting/apps',
data: data,
processData: false,
contentType: false,
timeout: 600000,
beforeSend: function(xhr) {
xhr.setRequestHeader('X-Token-Id', getCookieToken());
xhr.setRequestHeader('X-Connector-Id', 'TestSeed');
},
success: function(data) {
console.log("SUCCESS : ", data);
},
error: function(e) {
console.log("ERROR : ", e);
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="POST" enctype="multipart/form-data" id="testFileUpload">
<input type="file" name="selectedFile" />
<input type="submit" value="Submit" id="btnSubmit" />
</form>
回答2:
You can try it in following way: Update your click handler to; jQUERY:
event.preventDefault();
var data = new FormData();
data.append("file",$('input[name="selectedFile"]').files[0]);
// your ajax call looks fine. Use the same.
回答3:
$("#contactform").validate({
errorClass: "errors_lable",
rules: {
email: {
required: true,
email: true,
},
},
messages: {
email: {
required: "Please enter email address.",
email: "Please enter Valid email address.",
},
},
submitHandler: function(form) {
try {
var formData = new FormData();
formData.append("LogoName", $('input[type=file]')[0].files[0]);
formData.append("email", isCollege);
waitingDialog.show('Please Wait While Processing...');
$.ajax({
type: "POST",
dataType: 'JSON',
url: "https://code0.ww.com/index.php/...../RegisterSchool",
data: formData,
processData: false,
contentType: false,
success: function(data) {
waitingDialog.hide('Please Wait While Processing...');
if (data.ResponseHeader.response_type == 'success') {
// $('#msg').show();
// $('#msg').html("<span style='color:#b9efb9'><b>Thankyou For Register</b></span>");
alert('Thankyou For Register');
} else if (data.ResponseHeader.response_type == 'error') {
//$('#msg').show();
//$('#msg').html("<span style='color:red'><b>Opps! There is some error, Please Try again latter.</b></span>");
alert('Somithing Went Wrong Please Try Again Latter');
}
},
error: function(data) {
alert('Somithing Went Wrong Please Try Again Latter');
}
});
} catch (err) {
console.log(err);
}
}
});
来源:https://stackoverflow.com/questions/50937704/jquery-file-upload-using-formdata-not-working