简体   繁体   English

用Ajax发布CVS数据

[英]Post cvs data with ajax

Im trying to post data with ajax but in console i recive something like.. Uncaught ReferenceError: file is not defined 我试图用ajax发布数据,但在控制台中我收到类似..未捕获的ReferenceError:文件未定义

This is my input with my button to upload data: 这是我用按钮上传数据的输入:

<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>

This is my jquery/ajax: 这是我的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");
                                }
                            });
                        }
                    }
                });

But the data is not POST i think is something missing 但是数据不是POST,我认为缺少了一些

You have created a variable called files , but are using a variable called file . 您已经创建了一个名为files的变量,但是正在使用一个名为file的变量。 Try changing this portion of your code: 尝试更改代码的这一部分:

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");
        }
    });
}

This just renames the files variable to file and changes the one other place that used files . 这只是将files变量重命名为file并在另一个使用files地方进行了更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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