gpt4 book ai didi

javascript - 使用 AJAX 值的文件上传未从其他字段传递

转载 作者:行者123 更新时间:2023-11-30 16:10:47 25 4
gpt4 key购买 nike

我正在使用 AJAX 文件上传,它应该工作正常。我有两个表单字段 firstname 和文件上传。从这个代码文件上传值我可以得到但是名字值没有进入下一页(upload.php)而使用 AJAX...

<form class="form-horizontal form-bordered" method="POST" id="newUserForm" enctype="multipart/form-data">
<div class="form-group">
<label class="col-md-3 control-label">First Name<span class="star_mark">&nbsp;*</span></label>
<div class="col-sm-6">
<input type="text" class="form-control" id="fname" name="fname" value="" aria-required="true" required="" data-msg-required="Please enter your firstname" placeholder="Enter your firstname">

</div>
</div>

<div class="form-group">
<label class="col-md-3 control-label">Photo Upload<span class="star_mark">&nbsp;*</span></label>
<div class="col-md-6">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="input-append">
<div class="uneditable-input">
<i class="fa fa-file fileupload-exists"></i>
<span class="fileupload-preview"></span>
</div>
<span class="btn btn-default btn-file">
<span class="fileupload-exists">Change</span>
<span class="fileupload-new">Select file</span>
<input type="file" id="file" name="file" value="" aria-required="true" required="" data-msg-required="Please select your file">
</span>

<a href="#" class="btn btn-default fileupload-exists" data-dismiss="fileupload">Remove</a>
</div>
</div>
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button class="btn btn-info btn-block" type="submit" id="user-submit">Submit</button>
</div>
</div>
</form>
</div>
</div>

<script type="text/javascript">
$(document).ready(function(){
$("#user-submit").click(function(event){
event.preventDefault();

if($("form#newUserForm").valid()){

// var formData = new FormData($(this)[0]);
var formData = new FormData();
formData.append('file', $('input[type=file]')[0].files[0]);
$.ajax({
url: 'upload.php',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function(data){
alert(data)
},
});


return false;
}else{
console.log("false");
}
});
});
</script>

upload.php

<?php

$fstname = $_POST['fname'];//here i can't get the first name value

if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}
else {
if( move_uploaded_file($_FILES['file']['tmp_name'], 'img/' . $_FILES['file']['name'])){
echo "upload success";
}else{
echo "upload Error";
}
}

最佳答案

试试这个

function uploadFile(blobFile, fileName){
var fd = new FormData();
fd.append("fileToUpload", blobFile);

$.ajax({
url: "upload.php",
type: "POST",
data: fd,
processData: false,
contentType: false,
success: function(response){
// .. do something
},
error: function(jqXHR, textStatus, errorMessage){
console.log(errorMessage); // Optional
}
});
}

关于javascript - 使用 AJAX 值的文件上传未从其他字段传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36279828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com