【问题标题】:Post cvs data with ajax使用 ajax 发布 csv 数据
【发布时间】:2016-05-31 23:44:10
【问题描述】:

我正在尝试使用 ajax 发布数据,但在控制台中我收到类似.. Uncaught ReferenceError: file is not defined

这是我通过按钮上传数据的输入:

<div id='file_browse_wrapper'>
  <input class="iconos" type="file" name="File Upload" id='file_browse' accept=".csv" />
</div>
  <label id="url-archivo"><b>Selecciona tu archivo...</b></label>
  <button class="btn btn-success" id="carga-archivo">Subir</button>

这是我的 jquery/ajax:

$("#carga-archivo").click(function () {
                    var fileUpload = document.getElementById("file_browse");
                    if (fileUpload .value != null) {
                        var uploadFile = new FormData();
                        var files = $("#file_browse").get(0).files;
                        // Add the uploaded file content to the form data collection
                        if (files.length > 0) {
                            uploadFile.append('file-'+i, file);
                            $.ajax({
                                url: "/2/delivery/client/massive",
                                contentType: false,
                                processData: false,
                                data: uploadFile,
                                type: 'POST',
                                success: function () {
                                   alert("file upload successfuly");
                                }
                            });
                        }
                    }
                });

但数据不是 POST 我认为缺少一些东西

【问题讨论】:

  • 正是它所说的。你在哪里设置/定义file
  • 我从我的输入中捕获文件
  • 你没有。变量file 未定义。

标签: javascript jquery ajax


【解决方案1】:

您创建了一个名为files 的变量,但正在使用一个名为file 的变量。尝试更改这部分代码:

var file = $("#file_browse").get(0).files;
// Add the uploaded file content to the form data collection
if (file.length > 0) {
    uploadFile.append('file-'+i, file);
    $.ajax({
        url: "/2/delivery/client/massive",
        contentType: false,
        processData: false,
        data: uploadFile,
        type: 'POST',
        success: function () {
           alert("file upload successfuly");
        }
    });
}

这只是将files 变量重命名为file 并更改另一个使用files 的位置。

【讨论】:

  • @victor 如果这回答了您的问题,请单击答案旁边的复选标记以接受它。复选标记颜色将变为绿色,表明它已被接受。如果您有任何问题或有新信息,请告诉我,我可以提供更新的答案。
猜你喜欢
  • 1970-01-01
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多