简体   繁体   中英

Rendering CSV data as HTML

I am trying to get the jquery csv to table plugin working with the jquery file upload plugin (see http://code.google.com/p/jquerycsvtotable/ ). It supports uploading a CSV file and upon successful upload (using JQuery File upload) immediately rendering it into HTML on the same page. This works fine when I set the $filePath as per example below. However, I wish to pass in this value as a hidden field and retrieve it by using the .attr method. However when I do this the CSV to table does not seem to render properly and the page seems to hang with the loadingImage (update still works), and the 'loadComplete' function is not executed (as per alert in example below).

I can get it working as follows (see the lines in bold):

    }).bind('fileuploadcompleted', function (e, data) {        
        var $filename = $.trim($('#fileupload td.name').text());
        **var $filePath = 'bundles/jqueryfileupload/users/';
        //var $filepath = $('#upload_path_id').attr('value');**
        $.get($filePath + $filename, function(data) {
            $('#CSVSource').html('<pre>' + data + '</pre>');
        });
        $('#CSVTable').CSVToTable($filePath + $filename,{
          loadingImage: 'bundles/jqueryfileupload/img/loading-table.gif',
          startLine: 0 }             
        ).bind("loadComplete",function() {
              alert('load complete!');
              $('button.btn.import').removeClass('disabled').addClass('ready');
              $('div.alert.alert-info').show();
        });

Any idea how I can get around this issue?

UPDATE: It looks like the issue exists because I am retrieving an absolute path and this is failing with the ajax call that renders the csv to HTML. I'm using Laravel and have updated the paths.php file to include the following entry:

// --------------------------------------------------------------
// The path to the storage/users directory.
// --------------------------------------------------------------
$paths['user_files'] = 'storage/users';

then in my view file I am passing in the file path as a hidden field as follows:

<input type="hidden" id="upload_path_id" name="upload_path" value={{ path('user_files') }}>

so my question is if I'm using both Laravel and Xampp locally on my machine is there a way to get this working with ajax either as a relative path or some other way? The folder I'm using above is part of Laravel (ie under 'storage') and is ABOVE the public directory.

Any help on this would be much appreciated.

Chatting on IRC I have had support suggesting to create a route to a controller (php) that will have access to the Storage directory and retrieve the file contents that way.

So instead of the current php -> js -> js scenario I would have the following: php (view) -> js (make ajax call) -> php (controller) -> obtain storage path -> php (controller) (perform file get contents) -> js (render the results as HTML)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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