我正在尝试通过AJAX Laravel(jquery ajax)发送图像上传 . 我总是收到空的$ _FILES数组 . 我在表单中添加了enctype =“multipart / form-data . 当我通过Laravel的post方法发送数据时,我得到了我的数据 . 这不适用于jquery ajax . 我正在获取其他我发送的其他内容的ajax响应$ _FILES .
我的查看文件
<form action="" name="myForm_comment" id="myForm" method="post" role="form" enctype="multipart/form-data">
<input type="hidden" name="snap" id="snap" value="">
<input type="hidden" name="op" id="op" value="">
<input type="hidden" name="ucname" id="name" value="{{$fname}} {{$lname}}">
<textarea rows="3" class="form-control" placeholder="Add a public comment" style="padding:10px;" name="ucmsg" id="textmsg" required></textarea>
<input type="file" id="imgInp" name="imgInp"/>
<img id="blah" src="" alt="" />
<div class="row">
<div class="col-xs-1 col-xs-offset-8 text-center">
<label for="imgInp"><i class="fa fa-camera-retro" aria-hidden="true"></i></label>
</div>
<!-- <div class="col-xs-1 text-center">
<label for="imgInp"><i class="fa fa-smile-o" aria-hidden="true"></i></label>
</div> -->
<div class="col-md-2">
<input type="submit" id ="cmnt_btn" class="btn btn-primary pull-right" name="commentSubmit" value="Comment" onclick="commentFunction();">
</div>
</div>
</form>
<script>
$(document).ready(function(){
//comment database management
$("#cmnt_btn").click(function(){
var name = $("#name").val();
var textmsg = $("#textmsg").val();
var picture = $("#imgInp").val();
$.ajax({
url:'comment_dbm',
type:'get',
data:{
ucname:name,
ucmsg:textmsg,
ucpic:picture,
},
success:function(response)
{
alert(response);
}
});
});
});
</script>
我的控制器
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
use DB;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Session;
use Illuminate\Contracts\Validation\Validator;
use Mail;
session_start();
class CommentController extends Controller
{
public function comment_dbm()
{
echo $ucname = $_GET['ucname'];
$ucmsg = $_GET['ucmsg'];
$cmnt_pic = $_GET['cmnt_pic']; //I am getting these values response
print_r($_FILES); // getting empty array
]);
}
}
我的路线档案(即在web.php中)
Route::get('/comment_dbm', [
'uses' => 'CommentController@comment_dbm',
'as' => 'comment_dbm'
]);
1 回答
您不能使用
session_start()
或$_GET
. 更好地使用内置的Session
和Request
类 . 此外,您无法通过GET请求传输数据(如图像) . 使用POST一个 .然后你可以按照答案here