【问题标题】:jQuery upload running but not uploading filejQuery上传正在运行但没有上传文件
【发布时间】:2014-12-15 02:01:54
【问题描述】:

我想知道是否可以得到一些建议。

我正在尝试使用 jquery 上传文件,同时将用户保持在同一页面上。

无论我尝试什么都行不通。我想知道是否有人可以看看并告诉我我哪里出错了。

我的 HTML 是

<form id="import" enctype="multipart/form-data" action="/ajax/postimport" method="POST">
    <div class="form-group">
        <input type="file" name="filename" id="filename" />
    </div>
    <button type="button" id="importSave">Import</button>
</form>

这是我的 jquery

$("#importSave").click(function()
{
    $.ajax({
        url: '/ajax/postimport',
        type: 'POST',
        dataType: 'json',
        contentType: false,
        processData: false,
        data: {file: $(#filename).val()},
        success: function(data)
        {
            alert(data.result)
        },
        error: function(textStatus, errorThrown)
        {

        }
    });
});

然后是我的 PHP,即 Laravel 4

        if (Input::hasFile('filename')) {
            $file = Input::file('filename');
            $destPath = public_path() . '/uploads/';
            $filename = str_random(10) . '_' . $file->getClientOriginalName();
            $uploadSuccess = $file->move($destPath, $filename);
        }

【问题讨论】:

    标签: php jquery laravel laravel-4


    【解决方案1】:

    仅使用 jQuery ajax 功能无法上传文件。您需要使用FormData 对象来执行此操作。 Formdata 的问题在于它是not supported in all browsers。如果你确实想使用它,你总能找到很多教程,比如this one

    您也可以使用a jQuery plugin that does the work for you。 :)

    【讨论】:

      【解决方案2】:

      这是不可能的。您只是使用一个字段文件发出后 ajax 请求:$(#filename).val()。

      所以你需要使用ajax上传库之类的。 有几种选择。但我最喜欢的是

      http://www.plupload.com/

      http://www.uploadify.com/

      【讨论】:

        猜你喜欢
        • 2013-11-12
        • 1970-01-01
        • 2017-03-15
        • 1970-01-01
        • 1970-01-01
        • 2012-10-24
        • 2014-02-03
        • 1970-01-01
        • 2016-07-28
        相关资源
        最近更新 更多