2016-03-26 24 views
0

AJAX kullanarak bir PDF dosyası yüklemeye çalışıyorum ama bilinmeyen bir hatayla başarısız olmaya devam ediyor. Neyi yanlış yapıyorum?AJAX kullanarak PDF yükleyin

HTML Dosyası:

<form id="document"> 
    <p> 
     Title<br> 
     <input type="text" name="name" size="30"> 
    </p> 
    <p> 
     Please specify a file, or a set of files:<br> 
     <input type="file" name="datafile" size="40"> 
    </p> 
    <div> 
     <input id="submit-button" type="button" value="Send"> 
    </div> 
</form> 

<script src="jquery.js"></script> 
<script> 
    $(document).ready(function(){ 
     $('#submit-button').click(function() { 
      $.ajax({ 
       type: "POST", 
       dataType: "JSON", 
       url: "upload_document.php", 
       data: $("#document").serialize(), 
       success : function(data){ 
        alert(data.message); 
       }, error : function(data) { 
        alert(data.message); 
       } 
      }); 
     }); 
    }); 
</script> 

PHP Dosyası (upload_document.php)

<?php 

header("Access-Control-Allow-Origin: *"); 

try { 
    $id = "[RANDOM_GENERATED_GUID]"; 

    $targetDir = "../../../../modules/sites/documents/"; 
    if (!is_dir($targetDir)) { 
     if (!mkdir($targetDir, 0777, true)) { 
      throw new Exception("Unable to upload your document. We were unable to create the required directories"); 
     } 
    } 

    $targetFile = $targetDir . $id . ".pdf"; 
    $fileType = pathinfo($targetFile, PATHINFO_EXTENSION); 

    if (file_exists($targetFile)) { 
     throw new Exception("Unable to upload your document. The file already exists"); 
    } 

    if ($_FILES["datafile"]["size"] > 2000000) { 
     throw new Exception("Unable to upload your document. The file is to large (Maximum of 2MB)"); 
    } 

    if ($fileType != "pdf") { 
     throw new Exception("Unable to upload your document. Only PDF documents can be uploaded"); 
    } 

    if (!move_uploaded_file($_FILES["datafile"]["tmp_name"], $targetFile)) { 
     //Keeps failing here with error code 0 
     throw new Exception("Unable to upload your document. There was an error uploading the file"); 
    } 

    echo json_encode(array(
     "error" => false, 
     "message" => "Your document was successfully uploaded" 
    )); 
} catch (Exception $ex) { 
    echo json_encode(array(
     "error" => true, 
     "message" => $ex->getMessage() 
    )); 
} 

Ben de sunucu üzerinde kontrol ve dizin başarıyla oluşturulmaktadır. Yardım için teşekkürler!

Düzenleme ben formdaki işlemini, bir Gönder düğmesini kullanırsanız bu tam aynı PHP komut dosyası çalışır
. AJAX kullanmak için tek neden, bir yanıt aldıktan sonra kalıcı bir iletişim kutusu görüntülemek için

+0

Seni oluşturulan ve o isme $ filetype alıyorsanız görüyoruz Bir problem yüklediğiniz dosyadan, bu $ $ gibi dosya türünde olması gerekir * pathinfo ($ _ FILES ["datafile"] ["tmp_name"], PATHINFO_EXTENSION); – kunicmarko20

+0

Teşekkürler, bunu deneyeceğim. Ama şunu da unutmuştum, eğer bir form kullanırsam, eylemi ayarla ve gönder butonunu kullan ( –

+0

) Bu şekilde bir dosyayı serileştiremezseniz, bu işlem mükemmel bir şekilde gerçekleşir. Tarayıcı güvenliği/korumalı alan izin vermiyor. Başka bir çözüm kullanmalısınız, muhtemelen bir iframe kullanarak bir şey. Buna bakın [link] (http://stackoverflow.com/a/4545089/1233305) – David784

cevap

0

AJAX POST kullanmaya başladığınızda, kaydedilen parametre $ _PILST yerine $ _POST olarak aranmalıdır.

Bunun nedeni, $ _FILES, çok parçalı posta yoluyla yüklenen dosyalar için bir önbellek olmasıdır. Eğer AJAX kullanarak bunu tefrika ve gönderilen beri, PHP JSON ayrıştırır ve $ _POST

bir örnek için here Look içindeki her şeyi koymak

+0

Yanıt için teşekkürler. $ _POST ile $ _FILES değiştirmeyi denedim, ama yine de aynı hatayı alıyorum –

+0

@HenryJooste Hangi kodun soruna neden olduğunu görmek için PHP kodunuzu ikiye bölmeyi deneyebilir misiniz? –

+0

Evet, bunu zaten yaptım. 'Move_uploaded_file' çağrısında başarısız olur. Dosyayı aldığından eminim, çünkü PDF dosya uzantısı kontrolünden geçiyor. –

İlgili konular