2017-08-30 41 views
11

Laravel观点:如何在laravel中使用ajax post请求将图像插入到db中?

<form enctype="multipart/form-data" method="post" name="imagesform" id="imagesform" > 
    <input type="hidden" name="_token" value="{{ csrf_token()}}"> 
    <div> 
     <label id="show" for="files" class="button">Upload photo</label> 
     <input id="files" name="images" style="visibility:hidden;" type="file"> 
    </div> 
    <button type="submit" class="save" id="saveImage" style="border-radius: 8px; padding: 5px 15px;">SAVE</button> 
</form> 

这是我上传图片的代码(它发生我的引导模式内)。当我上传的图片,并点击提交按钮,图像应插入到数据库并应该检索并显示在视图页面上。

Ajax代码:

$("#saveImage").click(function(e){ 
    // var formdata=new FormData($("#imagesform")[0]); 
    //alert(formdata); 
    $.ajaxSetup({ 
     headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') } 
    }); 
    $.ajax({ 
     url: 'saveImages/{{$dataId}}', 
     type: 'POST', 
     data:new FormData($("#imagesform")), 
     success: function (data) { 
      alert(data) 
     }, 
     cache: false, 
     processData: false 
    }); 
    return false; 
}); 

这Ajax代码,我已经tried.But FORMDATA的警报显示[对象FORMDATA]和浏览器控制台显示Method not allowed exception

Laravel路线:

Route::post('saveImages/{dataId}',['as' => 'saveImages','uses'=>'[email protected]']); 

控制器:

public function saveImages(Request $imagesform,$dataId) 
{ 
    if (Input::hasFile('images')) 
    { 
     $name = $this->getImageId().".".$imagesform->file('images')->getClientOriginalExtension(); 

     $image = $imagesform->file('images');   
     $resize = Image::make($image)->resize(700, 300)->encode($imagesform->file('images')->getClientOriginalExtension()); 
     $hash = md5($resize->__toString()); 

     $path = "public/" . $name; 
     Storage::put($path, $resize->__toString()); 
    } 
    $insertImages=new photosModel; 
    $insertImages->imagesid=$this->getimagesId(); 
    $insertImages->deviceCategoryId=$dataId; 
    $insertImages->uploadedImages=$name; 
    $insertImages->save(); 
    return $this->formImages($dataId); 

} 

public function formImages($dataId){ 
    $dataId=$dataId; 
    $images=photosModel::all(); 
    return view('addProductDetails1')->with('images',$images)->with('dataId',$dataId); 
} 
+0

'变种形式= $(” # var formData = new FormData(form);'用这个 –

+0

尝试同样的错误 –

+0

再次警告[object FormData] –

回答

6

要通过AJAX上传文件,你需要使用XHR Level 2。如果要发送其键值,则FormData类需要构造函数中的HTMLFormElement

你应该使用:

new FormData(document.getElementById('imagesform')); 

你也可以用jQuery的使用:

new FormData($("#imagesform").get(0)) 

检查my answer在另一个类似的问题,以获取更多信息。

更新: 看到您的意见您的问题是与Laravel路线太。如果错误代码是500,并且显示消息“方法不允许”,则需要检查routes/web.php中的路由。请在这里放置错误堆栈跟踪和路径文件(使用pastebin或类似的)。

+0

priceDetails是我的控制器名称Sir –

+0

line in the routes file ?? –

+0

我很抱歉我没有看到你的问题。请你可以把这个错误与stacktrace放在一起吗? –

3

我能够通过Frankensteining从here提出的各种建议来解决类似的问题。该链接有一堆非常好的信息。您需要将ajax调用中的某些选项指定为false。在任何情况下,这是我工作:

HTML

<input name="image" type="file" id="image"> 

的Javascript

data = new FormData(); 
    jQuery.each(jQuery('#image')[0].files, function(i, file) { 
     data.append('image', file); 
    }); 

$.ajax({ 
    url: "submit/ajax", 
    type: 'POST', 
    cache: false, 
    contentType: false, 
    processData: false, 
    data: data, 
    success: function(response) 
     { 
      // Display your uploaded image on the page 
     } 
    }); 

PHP

Image::make($_FILES['image']['tmp_name']) >save("$myImageDirectory/$newImagegName"); 
相关问题