我想使用模式上传图片。我知道如何使用HTML表单执行此操作,但是我不确定如何使用ajax请求将数据发送到控制器。是否可以使用我现在拥有的控制器?预先感谢!
控制器:
public function postCreatePost(Request $request)
{
...
$post->longitude = $request['longitude'];
if($request->user()->posts()->save($post))
{
$message = 'Post Successful';
};
$file = $request->file('image');
$filename = $post->id.'.jpg';
if($file)
{
Storage::disk('local')->put($filename, File::get($file));
}
return redirect()->route('dashboard')->with(['message' => $message]);
}
我到目前为止拥有的ajax请求:
$('#modal-save').on('click',function(){
//create ajax request
$.ajax({
method: 'POST',
url: urlEdit, //url route is defined in the dashboard html script
//pass the post content and id to body and postId which will be used in the Post controller edit method
//need the token to avoid error
data: {body: $('#post-content').val(), postId: postId, _token: token}
})
.done(function (msg){
//console.log(msg['message']);
//console.log(JSON.stringify(msg));
$(postBodyElement).text(msg['new_content']); //update the post w/o refreshing the page
$('#edit-post').modal('hide'); //hide the modal
})
});
模式:
<div class="modal" tabindex="-1" role="dialog" id="edit-post">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit Post</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form enctype="multipart/form-data">
<div class="form-group">
<label for="post-content">Edit Post</label>
<textarea class="form-control" name="post-content" id="post-content" rows="5"></textarea>
{{-- copy meta data using jquery?--}}
{{-- add a separate image form here--}}
<label for="image">Image (jpg only)</label>
<input type="file" name="image" class="form-control" id="image">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="modal-save">Save changes</button>
</div>
</div>
</div>
</div>
答案 0 :(得分:0)
控制器看起来不错。但是您没有传递图像:
尝试一下
$('#modal-save').on('click',function(){
//create ajax request
$.ajax({
method: 'POST',
url: urlEdit, //url route is defined in the dashboard html script
//pass the post content and id to body and postId which will be used in the Post controller edit method
//need the token to avoid error
data: {
body: $('#post-content').val(),
postId: postId,
_token: token,
image:$('#image').val()
}
})
.done(function (msg){
//console.log(msg['message']);
//console.log(JSON.stringify(msg));
$(postBodyElement).text(msg['new_content']); //update the post w/o refreshing the page
$('#edit-post').modal('hide'); //hide the modal
})
});
然后您可以在控制器中获取图像:
if( $request->hasFile('image') ) {
$file = $request->file('image');
$name = time().$file->getClientOriginalName();
$file->move(public_path().'/images/', $name);
}
答案 1 :(得分:0)
您没有将图像发送到服务器端。请使用formData对象将媒体发送到服务器。
/** New form data object */
var formData = new FormData();
/** append post content */
formData.append('postcontent', $('#post-content').val());
/** append post id */
formData.append('postId', postId);
/** append token */
formData.append('_token', token);
/** append image */
formData.append('image', $("input[type=file]")[0].files[0]);
在您的ajax调用中,以数据形式发送formData
$.ajax({
method: 'POST',
url: urlEdit,
data: formData,
cache: false,
contentType: false,
processData: false
}).done(function (msg){
$(postBodyElement).text(msg['new_content']);
$('#edit-post').modal('hide'); //hide the modal
})