dqeq885710 2011-02-14 03:22
浏览 59

使用JSON的PHP上传返回到jQuery(全局)变量

I need to take a CSV file from the client's machine and extract the data from it. I cannot save this CSV file, which is crucial (otherwise I would be laughing!). My PHP so far works like a charm:

upload.php

<?php
    $file = $_FILES['file'];
    if ($file['error'] === UPLOAD_ERR_OK) {
        $ext = substr($file["name"], strrpos($file["name"], '.') + 1);
        $maxSize = 3000000;

        //Check file extension, MIME-type and size limit (3 MB).
        if ($ext == "csv") {
            if ($file["type"] == "text/csv" ||
                $file["type"] == "text/comma-separated-values") {

                if ($file["size"] < $maxSize) {
                    //CSV -> JSON
                    $fileAsArray = Array();

                    if (($handle = fopen($file["tmp_name"], "r")) !== FALSE) {
                        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                            $fileAsArray[] = $data;
                            }
                                fclose($handle);
                            }
                            echo json_encode($fileAsArray);
                            die(json_encode($fileAsArray));
                    }
                    else {
                        echo "File size: ".$file["size"]."bytes<br>Limit: ".$maxSize." (3MB)";
                    }
                }
                else {
                    echo "MIME-type: ".$file["type"]."<br>Required: text/csv";
                }
            }
            else {
                echo "File Extension: ".$ext."<br>Required: csv";
            }
        }
        else
            die("Cannot upload");
?>

Ignore the echo. It was just so I knew it was actually working. However, I've been using jQuery with this website and can't figure out the code to properly get the form to submit the file and retrieve the JSON data back (fileAsArray) at the same time. Here is my HTML/jquery in index.html:

<script type="text/javascript">
    var csvData = {}; //My global variable (two-dimensional JSON array)

    $(document).ready(function(){
        $("#upload").click(function(){
            alert('test');

            $.ajax({
                type: "POST",
                url: "upload.php",
                data: "don't know what goes here",
                dataType: "json",
                success: function (data) {
                    alert(data); //Tried everything here too. :(
                }
            });
        });
    });

and HTML:

<form method="POST" action="upload.php" enctype="multipart/form-data" class="file_upload">
    <input id="getFile" type="file" name="file">
    <input value="Extract Data" id="upload" type="button">
</form>

I tried type="submit" as well, and a bunch of other things including just a 'form' tag and letting jQuery handle the POST... Thanks. If there is a better way to do this, please let me know :)

(I've been struggling with this for several days now... Having gone through many renditions of the code outlined below, I've stripped it down to its basics.)

  • 写回答

1条回答 默认 最新

  • dongpaipu8394 2011-02-14 03:35
    关注

    If you want to upload CSV data through an Ajax post request, you must read the content of the file and insert it into the data: "don't know what goes here" field. Currently, only HTML5 browsers provides FileReader API that enables JavaScript to read the file, for Internet Explorer you must use a Flash-based solution.

    I wrote a jQuery plug-in before that works with HTML5 browsers and jQuery. It would work with jQuery 1.4 - I am not sure about jQuery 1.5.

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建