【问题标题】:ajaxForm submission with multiple file uploads带有多个文件上传的 ajaxForm 提交
【发布时间】:2017-02-28 17:16:53
【问题描述】:

过去几天我试图弄清楚如何使用 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 jquery file-upload ajaxform


    【解决方案1】:

    在你的 php 代码中写着:

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

    然后尝试删除该行:

    die('Success! File Uploaded.');
    

    (或将其注释掉!)

    有效吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 2023-03-26
      相关资源
      最近更新 更多