簡體   English   中英

帶有多個文件上傳的ajaxForm提交

[英]ajaxForm submission with multiple file uploads

我花了最后幾天試圖弄清楚如何使用ajaxForm插件上傳多個文件而沒有成功。 即使我從整體上找到了一些示例,它們還是沒有太大幫助,或者它們顯示的是單文件上傳,我設法使它按原樣工作,但不適用於多個文件。

這是HTML代碼

<div id="upload-wrapper">
 <div align="center">
 <form action="driverLoads/fetchDrivers/personnelUploads/processupload.php" method="post" enctype="multipart/form-data" id="uploadForm">
   <input name="FileInput[]" id="FileInput" type="file" multiple/>
   <input type="submit"  id="submit-btn" value="Upload" />
   <img src="driverLoads/fetchDrivers/personnelUploads/images/ajax-loader.gif" id="loading-img" style="display:none;" alt="Please Wait"/>
 </form>
 <div id="progressbox" ><div id="progressbar"></div ><div id="statustxt">0%</div></div>
 <div id="output"></div>

和javascript代碼:

$(document).ready(function() { 
 $('#submit-btn').click(function(e) { 
        e.preventDefault();
        $('#uploadForm').ajaxForm({
            target:   '#output',   // target element(s) to be updated with server response 
            beforeSubmit:  beforeSubmit,  // before submission callback function 
            success:       afterSuccess,  // after submission callback
            uploadProgress: OnProgress, //check on the upload progress
            resetForm: true,        // reset the form if upload is success 
            data:{driverid:driverid} //send the driverid as well
        }).submit();             
    }); 

和PHP代碼:

if(isset($_FILES["FileInput"])){
   $UploadDirectory =  $base_url . '/subSystems/drivers/driverLoads/fetchDrivers/personnelUploads/';

for($i = 0; $i < count($_FILES["FileInput"]['name']);$i++){
//check if this is an ajax request
   if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
       die();
    }


//Is file size is less than allowed size.
    if($_FILES["FileInput"]["size"][$i] > 5242880) {
    die("File size is too big!");
  }

//allowed file type Server side check
switch(strtolower($_FILES['FileInput']['type'][$i]))
    {
        //allowed file types

        //disabled till further notice
        case 'image/png': 
        case 'image/gif': 
        case 'image/jpeg': 
        case 'image/pjpeg':
        case 'text/plain':
        case 'text/html': //html file

        case 'application/x-zip-compressed':
        case 'application/pdf':
        case 'application/msword':
        case 'application/vnd.ms-excel':
        case 'video/mp4':
            break;
        default:
            die('Unsupported File!'); //output error
}

     if(move_uploaded_file($_FILES['FileInput']['tmp_name'][$i], $UploadDirectory.$_FILES['FileInput']['name'][$i] )){
        die('Success! File Uploaded.');
    }else{
       die('Error uploading File!');
    }

  }

 }else{
     $error = codeToMessage($_FILES["FileInput"]["error"][$i]);
    die('Something wrong with upload! Is "upload_max_filesize" set correctly?' . $error);
 }

我認為這是所有要理解的問題的相關代碼。 再次上傳一個文件時,代碼工作正常。 請注意,如果我上傳該代碼也可以正常工作,例如為了爭辯3個文件,但是如果僅上傳第一個文件,如果有人對此有更多的經驗,請幫忙,因為我似乎無法發現錯誤。

先感謝您。

在您的php代碼行中說:

if(move_uploaded_file($_FILES['FileInput']['tmp_name'][$i], $UploadDirectory.$_FILES['FileInput']['name'][$i] )){
        die('Success! File Uploaded.');

然后嘗試刪除該行:

die('Success! File Uploaded.');

(或將其注釋掉!)

它行得通嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM