有人请帮忙!这是我在Laravel得到的一个疯狂的错误。好吧,我有一个表单,我正在尝试将.doc .pdf和图像文件上传到服务器。所以在我的html中,我使用type属性将输入作为文件欺骗了。当我尝试使用ajax new FormData()提交表单时,我看到该文件是在控制台的HTTPS请求的标头中发送的,但是来自服务器的响应根本没有显示该文件。下面看看我的HTML中的代码。
<form id="form" method = "POST" enctype="multipart/form-data" action="{{url('/SetUp/Save')}}" >
<div id = "box" class="container dark well dataBox textBox2">
<script type="text/javascript">
num = 1; // only show the first box and hide the others VM keeping the num var for use later
</script>
@for($i = 1; $i <= $amount; $i++)
<div id = {{"panel".$i}} class="panel panel-default">
<div class="panel-heading">
<button type="button" class="btn btn-danger left">Remove
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</button>
<h3 class="panel-title"><strong> Location {{$i}}</strong></h3>
</div>
<div class=" pBody panel-body">
<div class="row">
<div class="form-group col-lg-6">
<label class = "control-label" label-default="Address" for="Address">Address
</label>
<input type="text" name= "Address[{{$i}}]" class="form-control" placeholder="Address">
</div>
<div class="form-group top col-lg-6">
<label class = "control-label" label-default="City" for="City">City</label>
<input type="text" name = "City[{{$i}}]" class="form-control"
placeholder="City">
</div>
</div>
<div class="row">
<div class="form-group col-lg-6 ">
<label class = "control-label" label-default="Picture" for="Picture">Picture</label>
<input type="file" name = "Picture[{{$i}}]" class= "form-control" id="Picture"
placeholder="Picture">
</div>
控制器
public function saveData(PostRequest $request) {
}
发布请求
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Log;
class PostRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function wantsJson()
{
return true;
}
public function response(array $errors)
{
return response()->json($errors);
}
public function rules() {
$rules = [];
var_dump($this->request); // request object print out
var_dump($this->request->get('Picture'); // return NULL to console
foreach($this->request->get('Picture') as $key => $val) // error 500 meaning Picture is null.
{
$rules['Picture.'.$key] = 'required|image';
}
}
正如您所看到的,我正在尝试验证请求,但最终出现错误。 当我var_dump请求时,我得到null并且它没有出现在var_dump这里看看var_dump打印出来
object(Symfony\Component\HttpFoundation\ParameterBag)#194 (1) {
["parameters":protected]=>
array(15) {
["Address"]=>
array(5) {
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
}
["City"]=>
array(5) {
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
}
}
这是我的ajax
/////////////send data to server
$("#form").on("submit",function(event) {
console.log("Fire");
$.ajax({
type: 'POST',
url: '/SetUp/Save',
//data: $(this).serialize(), // put the data in url form // send data over
data: new FormData( this ), // send data including file input to xml to process for sever
processData: false,
contentType: false,
dataType: 'json',
success: function(data){
HolderArray = []
JsonKeys = Object.keys(data);
for(x in data){
HolderArray.push(data[x])
}
for (var i = 0; i < JsonKeys.length; i++) {
holder = JsonKeys[i].replace(".","[");
$("[name='"+holder+"]'"+"]").parent().addClass(' animated shake has-error') .append(' <span class="help-block"> <strong>'+HolderArray[i]+'</strong></span>')
}
},
error: function(data){
var errors = data.responseJSON;
console.log(errors);
// Render the errors with js ...
}
});
return false;
});
找不到图片。看起来请求对象没有得到文件输入。我一直在网上搜索,没有运气解决这个问题。请帮助我是Laravel的新手,所以请帮帮我。谢谢
答案 0 :(得分:0)
$this->request->get('Picture'); // this will return null
// But
$this->file('Picture');// will return the file and show that the file has been uploaded. It seems like the file does not bind the to request object.
现在而不是像这样验证我的字段
foreach($this->request->get('Picture') as $key => $val)
{
$rules['Picture.'.$key] = 'required';
} // this gives a null error
我改变了这个并且它有效
foreach($this->file('Picture') as $key => $val)
{
$rules['Picture.'.$key] = 'required';
}
它有效!!! 。
感谢您的帮助!!!