stackexchange 中已经有很多关于这个主题的问题。我已经尝试了所有这些,但我无法上传文件。
HTML
<form id="getjs">
<input type="file" name="icon_next" id="icon_next">
</form>
<button type="submit" id="submit2" class="btn btn-primary">Submit</button>
查询
$(document).on('click', '#submit2', function(e) {
var icon_next = $('#icon_next')[0].files[0];
//have try FormData('#getjs')
//have try $('#icon_next').serialize();
console.log(icon_prev);// this give File { name: "filter_function - Copy.png", lastModified: 1557632103057, webkitRelativePath: "", size: 91746, type: "image/png" }
var c = $.ajax({
type: 'POST',
url: 'custom_setting/loadicon',
data: {'file': icon_next},
contentType: false,
processData: false,
success: function(response) {
//this always success
}
});
});
函数custom_setting/loadicon
if (isset($_GET['file'])) {
$k = 1;
// Code for upload file
} else {
$k = 0;
}
我的问题总是得到 $k=0; 没有文件被传送。我哪里错了?
试试这个代码
$(document).on('click', '#submit2', function(e) {
let files = new FormData(), // you can consider this as 'data bag'
url = 'custom_setting/loadicon';
files.append('fileName', $('#icon_next')[0].files[0]); // append selected file to the bag named 'file'
$.ajax({
type: 'post',
url: url,
processData: false,
contentType: false,
data: files,
success: function (response) {
console.log(response);
},
error: function (err) {
console.log(err);
}
});
});
自定义设置/加载图标
if (isset($_FILES) && !empty($_FILES)) {
$file = $_FILES['fileName'];
print_r($file);
$name = $file['name'];
$path = $file['tmp_name'];
// process your file
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句