【问题标题】:file upload validation with AJAX使用 AJAX 进行文件上传验证
【发布时间】:2014-08-27 06:48:33
【问题描述】:

我正在尝试对文件上传进行验证。我正在使用 AJAX 执行此操作,但在验证过程中出现问题。

jQuery('#form-docs').submit(function()
{
    var url = $(this).attr("action");
    jQuery.ajax({
        url: url,
        type: "post",
        data: jQuery('#form-docs').serialize(),
        datatype: "json",
        beforeSend: function()
        {
            jQuery('#ajax-loading').show();
            jQuery(".validation-error-inline").hide();
        }
    })
            .done(function(data)
            {
                $('#validation-div').empty()
                if (data.validation_failed === 1)
                {
                    var arr = data.errors;

                   alert(arr);
                }
                else {
                    window.location = data.redirect_to;
                }
            })
    return false;
});

控制器

 if ((Input::file('owner_cert_doc') != NULL) || (Input::file('sgs_doc') != NULL) || (Input::file('tpl_doc') != NULL) || (Input::file('kasko_doc') != NULL) || (Input::file('drive_permis_doc') != NULL)) {
            $car_id = Input::get('id_value');
            $id = DB::select('select id from insur_docs where car_id=?', array($car_id));
            $id_insert = DB::select('select id from insurdocs_inserts where car_id=?', array($car_id));
            $options = ['gs' => ['Content-Type' => 'text/plain']];
            $ctx = stream_context_create($options);
            //ownership certificate documentations
            $random_owner = str_random(5);
            if (Input::hasFile('owner_cert_doc')) {
                $owner_input = Input::get('owner_cert_doc');
                $owner_cert_doc = $_FILES['owner_cert_doc']['name'];
                if (false == rename($_FILES['owner_cert_doc']['tmp_name'], 'gs://docs_upload/' .
                                $random_owner . '_' . $owner_cert_doc, $ctx)) {
                    die('Could not rename.');
                }
            }
 $car = InsurDoc::find($id[0]->id);
            if (Input::hasFile('owner_cert_doc')) {
                $car->owner_cert_doc = 'gs://docs_upload/' . $random_owner . '_' . $owner_cert_doc;
            }
$car->save();

            return Redirect::to('car/' . $car_id);
        } else {        
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => Lang::get('messages.invalid_upload'),
                );
                return Response::json($response_values);   
        }

问题是当点击提交时没有任何反应。控制台显示:"Post 400 (bad request) 相反,如果将 else 替换为 dd('error');它显示“错误”。

【问题讨论】:

  • jQuery('#form-docs').serialize() 在进行 AJAX 调用时不会向服务器发送任何文件。
  • 但在其他验证中这有效。仅用于文件上传不
  • 是的,那是因为您无法使用 AJAX 上传文件。您可以使用本机 XmlHttpRequest 对象,如果客户端浏览器支持上传 HTML5 文件,您将能够实现这一点。否则,如果客户端使用的是旧版浏览器,您将不得不求助于一些传统技术,例如 Flash 或隐藏 iframe。
  • 还有什么其他选项可以显示没有 AJAX 的验证?如果没有提交文件,我只想显示一条消息
  • 没有太多选择。它将取决于浏览器及其支持的功能。如果支持,您必须检测客户端浏览器功能并执行验证。

标签: jquery ajax laravel laravel-4


【解决方案1】:

Darin Dimitrov 的 cmets 是正确的,为此您可以使用 http://www.plupload.com/ 或类似的插件来进行 AJAX 文件上传。他们的文档中有前端和后端的示例。很简单:)

【讨论】:

  • 我不想更改所有文件上传,直到我意识到这一点之前我遇到了一些困难。现在我只想在没有文件上传时显示错误消息。我还有什么其他选择?没有 AJAX
  • 令人困惑的问题。如果它是一个简单的 php 应用程序,我会说 - 像这样:w3schools.com/php/php_file_upload.asp,但您似乎正在使用某种框架,并且可能必须使用它的方法来显示验证错误。
猜你喜欢
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多