0

我想在我的应用程序中添加一个导入按钮以将文件导入服务器,之后我将自己处理该文件。这意味着我只想重用导入按钮和导入对话框,但在 OroCRM 中,我必须使用 OroCRM 的处理器和导入服务。如何不使用 OroCRM 导入文件的方式只使用导入按钮和导入对话框?十分感谢。:)

4

1 回答 1

0

如果您不想使用 OroCrm 导入。然后你可以像这个过程一样做。它可能会帮助你。

按着这些次序。

Step.1(index.html.twig)

{% set html %}
{{ UI.dropdownItem({
     'path': '#',
     'aCss':  'import',
     'title': 'Import File',
     'label': 'Import File',
     'iCss': 'fa-download'
}) }}

{% endset %}

{{ UI.dropdownButton({
   'label': 'Import File',
   'iCss': 'fa-download',
   'aCss': 'pull-right',
   'html': html
}) }}

第 2 步(在 index.html.twig 中)。创建模态部分。

{# Modal popup starts here #}
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title file-add-h">Select Import File</h4>
                <h4 class="modal-title file-report-h" style="display:none;">Import File Report</h4>
            </div>
            <div class="modal-body">
                <div id="file-add">
                <input type="file" name="File Upload" id="txtFileUpload" accept=".csv" />
                <button type="button" class="btn btn-info btn-lg pull-right submit-file">Submit</button>
                </div>
                <div id="file-report" style="display:none;">               </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
 </div>
{# modal popup end here #}

Step.3(在javascript文件中)

{# javascript #}

// Set the target
$(document).ready(function () {
    $(".import").attr('data-toggle', 'modal');
    $(".import").attr('data-target', '#myModal');
});

// Method that checks that the browser supports the HTML5 File API
        function browserSupportFileUpload() {
            var isCompatible = false;
            if (window.File && window.FileReader && window.FileList && window.Blob) {
                isCompatible = true;
            }
            return isCompatible;
        }

        function importFile(flag) {
            if (!browserSupportFileUpload()) {
                alert('The File APIs are not fully supported in this browser!');
            } else {
                var data = null;
                var file = $('#txtFileUpload')[0].files[0];
                var reader = new FileReader();
                reader.readAsText(file);
                reader.onload = function (event) {
                    var csvData = event.target.result;
                    data = $.csv.toArrays(csvData);
                    if (data && data.length > 0) {
                        //console.log(data);
                        var jsonArr = [];
                        for (var i = 0; i < data.length; i++) {
                            if (i !== 0)
                            {
                                var json_obj = {};
                                for (var j = 0; j < data[0].length; j++) {
                                    json_obj[data[0][j]] = data[i][j];
                                }
                                jsonArr.push(json_obj);
                            }
                        }

                        $.ajax({
                            url: '{{path('your_import_controller_path')}}',
                            type: 'POST',
                            data: {'data': JSON.stringify(jsonArr), 'flag': flag},
                            success: function (response, status, xhr) {
                                alert(response);
                            }
                        });

                    } else {
                        alert('No data to import!');
                    }
                };
                reader.onerror = function () {
                    alert('Unable to read ' + file.fileName);
                };
            }
            $('#txtFileUpload').val("");
            $("#file-report").html("");
            $(".file-add-h").show();
            $(".file-report-h").hide();
            $("#file-add").show();
            $("#file-report").hide();
            $('#myModal').modal('toggle');
        }

        $(document).on("click", ".submit-file", function () {
            importFile(0);
        });

        $(document).on("click", ".inser-file", function () {
            importFile(0);
        });

{# 然后在你的控制器动作中#}

 /**
 * @Route("/import",name="your_import_controller_path")
 */
public function importDataAction(Request $request)
{
    $import_data = $request->request->get('data');
    $flag = $request->request->get('flag');

    $import_arr = json_decode($import_data);
    $em = $this->container->get('doctrine')->getManager();

    $message = '';

    foreach ($import_arr as $i) {
        $tbl = new EntityTableName(); // in which entity you want to insert
        $tbl->setCode($i->CodeA); // CodeA is columns from excel

        $em->persist($tbl);
    }
    $em->flush();
    echo 'Records Imported Successfully';

    exit;
}
于 2017-05-02T11:15:19.893 回答