带有ajax请求的laravel文件上传(base64概念)

时间:2015-11-02 07:25:59

标签: php ajax laravel image-upload

任何人都可以帮我找一些使用AJAX REQUEST在Laravel 5中上传文件的演示或脚本吗?我已经完成了简单的提交页面,我只需要上传ajax请求文件的代码或演示。并使用base64概念会更有帮助。在此先感谢大家。

1 个答案:

答案 0 :(得分:0)

将此图像编码为控制器文件中的base64。我不是在写这段代码

//控制器文件

use File;
use Response;
class JsUpload extends Controller {
public function store(Request $request){
    $data = array();
    if($request->hasFile('image_file')){
        $file = Input::file('image_file');
        $name = time().'_'.$file->getClientOriginalName();
        $file = $file->move(public_path().'/images/',$name);

        //store $name into database;

        return Response::json([
            'success' => true,
        ]); 

    }
    else{
        return Response::json([
            'success' => false,
        ]); 
    }
}}

//路线档案

Route::post('uploadviajs','JsUpload@store');

// HTML FORM

<form method="post" enctype="multipart/form-data">
<input type="file"  name="image_file" id="image_file" >
<button id="imageupload">Upload</button>
</form>

//脚本文件

<script>
$('#imageupload').click(function(e) {
var myFormData = new FormData();
myFormData.append('image_file', image_file.files[0]);
$.ajax({
    url: "{!! url('artistProfileImage') !!}",
    type: 'POST',
    data: myFormData,
    cache: false,
    processData: false,
    contentType: false,
    beforeSend: function() {    
    },  
    complete: function() {  
    },              
    success: function(json) {       
    },
    error: function(xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    }
}); });</script>