简体   繁体   English

带有多个文件上传的ajaxForm提交

[英]ajaxForm submission with multiple file uploads

I spent the last couple of days trying to figure how to upload multiple files with ajaxForm plugin without any success. 我花了最后几天试图弄清楚如何使用ajaxForm插件上传多个文件而没有成功。 Even though I have found some examples on the entire they are either not much help or they are showing single file uploads which I hava managed to make it work as such, but not with multiple files. 即使我从整体上找到了一些示例,它们还是没有太大帮助,或者它们显示的是单文件上传,我设法使它按原样工作,但不适用于多个文件。

Here is the html code 这是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>

and the javascript code: 和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();             
    }); 

and the php code: 和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);
 }

I think its is all the relevant code for the question to be understood. 我认为这是所有要理解的问题的相关代码。 Again the code works fine when I upload one file. 再次上传一个文件时,代码工作正常。 Please note that the code also works fine if I upload, let's say for the sake of arguement 3 files, but if will upload only the first one.If anyone has more experience on this please help because I cant seem to spot the error. 请注意,如果我上传该代码也可以正常工作,例如为了争辩3个文件,但是如果仅上传第一个文件,如果有人对此有更多的经验,请帮忙,因为我似乎无法发现错误。

Thank you in advance. 先感谢您。

In your php code on the line that says: 在您的php代码行中说:

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

Then try to remove the line: 然后尝试删除该行:

die('Success! File Uploaded.');

(or comment it out!) (或将其注释掉!)

Does it work? 它行得通吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM