【发布时间】:2014-09-25 22:00:26
【问题描述】:
我实现了一个 ajax 文件上传,如 http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax 中所述。在所有浏览器中一切正常,但 iOS 上的 Safari 除外。作为响应,我收到内部服务器错误。
我检查了我的服务器端代码,但没有任何问题。如果我在服务器端 PHP 脚本的第一行代码处编写 die();,我什至会收到错误消息。看起来好像 Safari 错误地上传了图片。也许有人可以帮我修复这个错误。提前致谢!
这是 HTML 代码:
<input type="file" id="pict" accept="image/*">
<input type="text" id="field1">
<input type="text" id="field2">
<button id="send">Send</button>
这是 Javascript:
var files = false;
$('#pic').on('change', prepareUpload);
function prepareUpload(event)
{
files = event.target.files;
}
$("body").on("click", "#send", function() {
uploadTB();
});
function uploadTB() {
var postdata = new FormData();
if (files !== false){
$.each(files, function(key, value)
{
postdata.append(key, value);
});
}
postdata.append("field1", $("#field1").val());
postdata.append("field2", $("#field2").val());
$.ajax({
url: 'http://example.org/index.php',
type: 'POST',
data: postdata,
cache: false,
processData: false,
contentType: false,
success: function(data)
{
console.log("sucess");
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log(jqXHR,textStatus,errorThrown);
}
});
}
【问题讨论】: