我试图通过 jQuery 将文件上传到我的远程服务器,但我似乎无法让它工作。
用户需要能够上传 pdf 文件,然后由服务器处理并保存到根文件夹。
jQuery
$('form').submit(function (e) {
var fd = new FormData();
fd.append('file', $('#file')[0].files[0]);
$.ajax({
cache: false,
beforeSend: function (xhr) {
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("pragma", "no-cache");
},
url: 'http://www.codekraken.com/testing/pointify/test.php?callback=?',
data: fd,
dataType: "json",
processData: false,
contentType: false,
type: 'POST',
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
e.preventDefault();
});
PHP
<?php
header('content-type: application/json; charset=utf-8');
$name = $_FILES["fd"]["name"];
echo ($_GET['callback'] . '('.json_encode($name).')');
?>
HTML
<form>
<input type="file" id="file" name="file">
<input type="submit">
</form>
当我提交文件时,例如input.pdf
然后按提交,我得到了响应(null)
。我希望得到文件的名称input.pdf
,这意味着我错过了这个过程中的关键步骤。