2016-01-21 82 views
3

我想上傳文件在服務器上的JavaScript,雖然,將圖像上傳到服務器通過JavaScript

<script> 
    function SubmitFormData9() { 
     var fileToUpload1 = $("#fileToUpload1").val(); 
     var fileToUpload2 = $("#fileToUpload2").val(); 
     var resumeid9 = $("#resumeid9").val(); 
     $.post("r_nine.php", { fileToUpload1: fileToUpload1 , fileToUpload2: fileToUpload2 , resumeid9 : resumeid9 }, 
     function(data) { 
     $('#results').html(data); 
     $('#myForm9')[0].reset(); 
     }); 
    } 
    </script> 

    <form id="myForm9" method="post" enctype="multipart/form-data">    
    <input class="form-control" type="file" name="fileToUpload1" id="fileToUpload1" > 
    <input class="form-control" type="file" name="fileToUpload2" id="fileToUpload2" > 
<input id="resumeid9" name="resumeid9" type="hidden" value="<? echo $resumeid;?>"/> 
    <input type="button" class="btn btn-rounded btn-primary btn-sm" id="submitFormData9" onclick="SubmitFormData9();" value="Submit" /> 
    </form> 

,但我無法從形式值傳遞給r_nine.php頁

守則r_nine。 PHP頁面

$resumeid9 = $_POST['resumeid9']; 
    $target_dir = "reqdoc/"; 
    $target_file = $target_dir . basename($_FILES["fileToUpload1"]["name"]); 
    $uploadOk = 1; 
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
    $new_filename1 = $target_dir . uniqid() . '.' . $imageFileType; 
    if (move_uploaded_file($_FILES["fileToUpload1"]["tmp_name"], $new_filename1)) 
     { 
      $filee1 = $new_filename1; 
     } 


    $target_dir = "reqdoc2/"; 
    $target_file = $target_dir . basename($_FILES["fileToUpload2"]["name"]); 
    $uploadOk = 1; 
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
    $new_filename = $target_dir . uniqid() . '.' . $imageFileType; 
    if (move_uploaded_file($_FILES["fileToUpload2"]["tmp_name"], $new_filename)) 
     { 
      $filee = $new_filename; 
     } 

    $sql1="UPDATE resume set resume='".$filee1."',tracker_file='".$filee."' WHERE id='".$resumeid9."' "; 
    if(mysqli_query($con,$sql1)) 
     { 
     //echo "Files Saved Successfully"; 
     } 
    else 
     { 
     die('Error:' . mysqli_error($con)); 
     } 

誰能告訴如何糾正上面的代碼,這樣我可以上傳數據

+2

您需要使用'enctype =「multipart/form-data」'來上傳文件。並在ajax中使用'var formData = new FormData($(this)[0]);' – Saty

+0

[上傳兩個數據和文件在一種形式使用Ajax?](http://stackoverflow.com/questions/10899384 /上傳兩個數據和文件在一個形式使用ajax) – Saty

+0

而不是兩個文件類型使用一個與多個圖像上傳 –

回答

2
<script> 
    function SubmitFormData9() 
    { 
     var objFormData = new FormData(); 
     // APPEND FILE TO POST DATA 
     objFormData.append('fileToUpload1', $("#fileToUpload1")[0].files[0]); 
     objFormData.append('fileToUpload2', $("#fileToUpload2")[0].files[0]); 
     // APPEND TEXT TO POST DATA 
     objFormData.append('resumeid9', $("#resumeid9").val()); 

     $.ajax({ 
      url: 'r_nine.php', 
      type: 'POST', 
      contentType: false, 
      data: objFormData, 
      //JQUERY CONVERT THE FILES ARRAYS INTO STRINGS.SO processData:false 
      processData: false, 
      success: function (data) 
      { 

      } 
     }); 
    } 
</script> 

<form id="myForm9" method="post" enctype="multipart/form-data"> 
    <input class="form-control" type="file" name="fileToUpload1" id="fileToUpload1" > 
    <input class="form-control" type="file" name="fileToUpload2" id="fileToUpload2" > 
    <input id="resumeid9" name="resumeid9" type="hidden" value="<? echo $resumeid;?>"/> 
    <input type="button" class="btn btn-rounded btn-primary btn-sm" id="submitFormData9" onclick="SubmitFormData9();" value="Submit" /> 
</form> 

PHP代碼應該如下。

<?php 

function uploadFiles($strTargetDir = "", $strFileInputName = "") 
{ 

    $target_file = $strTargetDir . basename($_FILES[$strFileInputName]["name"]); 
    $uploadOk  = 1; 
    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); 
    $new_filename1 = $strTargetDir . uniqid() . '.' . $imageFileType; 
    if (move_uploaded_file($_FILES[$strFileInputName]["tmp_name"], $new_filename1)) 
    { 
     $filee1 = $new_filename1; 
    } 
} 

if (!empty($_FILES['fileToUpload1'])) 
{ 
    uploadFiles("reqdoc/", "fileToUpload1"); 
} 
if (!empty($_FILES['fileToUpload2'])) 
{ 
    uploadFiles("reqdoc2/", "fileToUpload2"); 
} 
?> 
+0

此代碼是否僅用於文件運行,還是可以發送文本輸入,例如if我需要發送名稱以及它我怎樣才能做到這一點 – sammy

+0

可以üplz告訴如何做到這一點, – sammy

+0

看到我更新的答案。 –

相關問題