Joomla上传图片问题

时间:2016-07-28 10:35:24

标签: php jquery ajax joomla

我为我的Joomla网站安装了BT Property组件,当我为我的文章选择多个图像(超过3或4个)时,它没有任何反应。图片无法上传。我尝试更改代码,但我不知道问题是什么。

这是我想要更改的代码。

<?php

defined('_JEXEC') or die('Restricted access');
$document = JFactory::getDocument();
$path = $this->params->get('images_path', 'images/bt_property');
?>

<ul class="adminformlist" id="uploading">
    <li><input type="file" name="attachment" id="attachment" multiple /><img id="spinner"
        style="display: none; margin-left: 10px;"
        src="<?php echo JURI::root(); ?>components/com_bt_property/assets/img/spinner.gif">
        <div style="clear: both"></div>
        <div id="btss-message"></div></li>
</ul>
<script type="text/javascript">
(function($){
            var files = [];
            $("#attachment").change(function(event) {
            $.each(event.target.files, function(index, file) {
              var reader = new FileReader();
              reader.onload = function(event) {
                object = {};
                object.filename = file.name;
                object.data = event.target.result;
                files.push(object);
                if(files.length==1){
                    uploadFile(index);
                    $('#spinner').show();
                    $("#btss-message").show();
                }
              };  
              reader.readAsDataURL(file);
            });
          });

          function uploadFile(index){
              $.ajax({url: "index.php?option=com_bt_property&task=properties.upload",
                    type: 'POST',
                    data: {filename: files[index].filename, filedata: files[index].data},
                    success: function(response, status, xhr){
                        uploadHandler(response, status, xhr);
                        if(index == files.length-1){
                            $('#spinner').hide();
                            files = [];
                            $("#attachment").val('');
                            $("#btss-message").delay(1000).slideUp(function(){
                                $("#btss-message").html('');
                            });
                        }else{
                            index++;
                            uploadFile(index);

                        }
                    }
              });
          }
          function uploadHandler(response, status, xhr) {
              var data = jQuery.parseJSON(response);
                      if(data == null){
                          showMessage("<br /><span style=\"color: red;\">Loading Failed</span>");
                      }else{
                          var file = data.files;
                          if (!data.success) {
                              showMessage("<br /><span style=\"color: red;\"> " + data.message +"</span>");
                          }
                          else {

                                var html = '<li>';
                                    html += '<input class="input-default" title="Make default" name="default_image" type="radio" value="'+file.filename+'" />';
                                    html += '<img class="img-thumb" src="<?php echo JURI::root() . $path . '/tmp/' . ($this->params->get('thumbimgprocess', 1) == -1 ? 'original' : 'thumb') . '-'; ?>'+file.filename+'" />';
                                    html += '<input type="hidden" name="image_id[]" value="0" />';
                                    html += '<input type="hidden" name="image_filename[]" value="'+file.filename+'" /><br/>';                                   
                                    html +='<a href="javascript:void(0)" class="edit" onclick="editImage(this)">Edit</a>';
                                    html +='<a href="javascript:void(0)" class="remove" onclick="removeImage(this)" >Remove</a>';
                                    html += '</li>';                                    
                                jQuery('#sortable').append(html);
                                jQuery('#sortable li:last-child ').find('a.edit').data('title', file.title);                                 
                                reNewItem();
                                showMessage('<br />' + file.title + " uploaded successfully!");
                          }

                      }
            }
          function showMessage(msg){
              $("#btss-message").append(msg);
          }

})(jQuery);

 </script>

有没有办法修复代码?我会非常感谢任何答案。

1 个答案:

答案 0 :(得分:0)

您的file对象即将显示为未定义,因此您无法使用file.filename

您的问题最有可能出现在这一行var file = data.files; - 您确定data.files吗?