【问题标题】:how to get the content of the file uploaded with input type file using JQuery如何使用 JQuery 获取使用输入类型文件上传的文件的内容
【发布时间】:2013-05-16 07:19:39
【问题描述】:

首先对不起我的英语。

如何在 Jquery 中访问使用 HTML->Input type="file" 标签上传的文件的内容。

表格代码是:-

<div id="import_csv_dialog" style='display:none'>
        <form method='post' enctype='multipart/form-data'>
        <table>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr><td>File Location:&nbsp;</td>
        <td><input type="file" name="file" id="file"/></td>
        </tr>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr><td colspan="2"><a style="cursor:pointer" class="css_btn" onclick="importCsv()">Import</a></td></tr>
        </table>
        </form>
        </div>

Jquery 代码是:-

function importCsv()
{
if($('#file').val()=="")
{
    jQuery.noticeAdd({
                    text: 'Please select a file',
                    type: 'error'
                });
    return;
}

var file_id=$('#file').val();
var url = "<?php echo $this->baseUrl; ?>/Ordermanagement/importcsv/"+file_id;
var data = {file_id: file_id};

$.post(url, data, function(data, textStatus, XMLHttpRequest)
{
    $('#gridList7').trigger("reloadGrid");
    jQuery.noticeAdd({
                      text: 'Data Imported successfully',
                      type: 'success'
                    });
    $('#import_csv_dialog').dialog('close');
});

}

在这段代码中,我以文件名作为参数调用 zend 控制器函数 importcsv。这是一个 .csv 文件。在这个 php 文件中,我将把这个 csv 文件的内容插入到我的数据库中。但是在这个函数中,我只能获取文件名作为参数,而不是内容或路径。我怎样才能得到这个文件的内容?这是在 $_FILES["file"]["tmp_name"] 的帮助下工作的简单 PHP。如何在 Jquery 中获取 tmp_name。

非常感谢

【问题讨论】:

标签: php jquery zend-framework


【解决方案1】:

<script type="text/javascript">
$("#form_oferta").submit(function(event) {
var dados = $( form ).serialize();
$.ajax({
type: "POST",
contentType:attr( "enctype", "multipart/form-data" ),
url: "<?php echo site_url(); ?>adm/oferta_insert",
data: dados,
success: function( data )
{
alert( data );
}
});
return false;
}
</script>

【讨论】:

  • 其实我是这个 zend/Jquery 的新手。所以请告诉我在哪里写这段代码。谢谢
  • 把这个放到你页面的head部分,还包括jquery的min文件
  • 在答案的一角单击上面的箭头符号,悬停在这两个箭头下方你会看到一个勾号,如果你愿意,可以两者都做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 2011-03-02
  • 2018-12-24
  • 2016-02-16
相关资源
最近更新 更多