【发布时间】: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