我試圖通過jQuery上傳文件到我的遠程服務器,但我似乎無法得到它的工作。jQuery使用FormData上傳PDF?
用戶需要能夠上傳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
,這意味着我錯過了這個過程中的關鍵一步。
您是否正在使用支持該類型上傳的瀏覽器進行測試? (除了IE <10) –
是的,Safari 6.0.2。 – Charlie
HTML正在從'localhost'運行,但是,如果那麼重要... – Charlie