2014-09-25 34 views
0

我實現了一個像http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax中所述的ajax文件上傳。所有瀏覽器中的一切正常,但iOS上的Safari。作爲迴應,我得到一個內部服務器錯誤。Ajax圖片上傳僅在iOS上失敗:內部服務器錯誤

我檢查了我的serverside代碼,但沒有錯。如果我在服務器端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); 
     } 
    }); 
} 

回答

0

更新的代碼

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: formData, 
     mimeType:"multipart/form-data", 
     contentType: false, 
     cache: false, 
     processData:false, 
     success: function(data) 
     { 
      console.log("sucess"); 
     }, 
     error: function(jqXHR, textStatus, errorThrown) 
     { 
      console.log(jqXHR,textStatus,errorThrown); 
     } 
    }); 
} 
+0

可悲的是,這並沒有解決它。要查看它是否上傳,我添加了一個XHR Progress Listener,它保持在0%,帶或不帶mimetype屬性。 – dboth 2014-09-25 12:35:29

+0

@dboth試着把你的所有元素放在一個表單中。希望這會起作用。 – vitorio 2014-09-25 12:50:31

+0

感謝您的幫助!但是,這一次的錯誤似乎是在蘋果方面.. – dboth 2014-09-25 14:02:13