使用jQuery ajax上传图像并使用干预api操作并保存到服务器

时间:2015-08-04 14:14:15

标签: image form-data laravel-5.1 php-5.6 intervention

首先让我解释一下我想要实现的目标。

用户填写表单并在提交时我想用jQuerys $ .post方法发送所有数据,然后使用Laravel 5.1和Intervention API(http://image.intervention.io/)我想处理数据并上传图像到服务器上的正确目录。

我的数据发送到服务器(本地),我可以使用Laravels Input类读取它,我认为问题在于我使用干预的方式,但我无法确定。

以下是我对表单的相关JS(删除了不相关的代码):

$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});

var State = {
    CurrentCategorySelected: 1
}

var URIROOT = '/administrator/';

$(document).on('click', '.ModalSave', function(e)
{   
  var fd = new FormData( $('.ProductForm').get(0) ),
        action = $(this).attr('data-saving');

    fd.append('category_id', State.CurrentCategorySelected);

    $.ajax({
        url: URIROOT+"new_product",
        type: "POST",
        data: fd,
        processData: false,
        contentType: false,
        success: function(response) {
            console.log(response);
        },
        error: function(jqXHR, textStatus, errorMessage) {
            console.log(errorMessage); 
        }
    }); 

    e.preventDefault();
});

这个JS似乎运行得很好但是我想我会发布它只是为了让你知道数据的来源。

接下来,我的Laravel路线如下:

Route::post('new_product', 'Admin\ProductManagerController@saveNewProduct');

控制器中的相关PHP如下(删除不相关的代码):

    <?php namespace TottonTimber\Http\Controllers\Admin;

    use View;
    use Image;
    use Input;
    use DB;
    use Illuminate\Http\Request;
    use TottonTimber\Http\Requests;
    use TottonTimber\Http\Controllers\Controller;
    use TottonTimber\Category;
    use TottonTimber\Product;

  class ProductManagerController extends Controller {
    public function saveNewProduct() 
    {
        /*
         * Define Varibles
         */
        $input = Input::all();
        $image = Input::file('main_image');
        $image_name = time().$image->getClientOriginalName();
        $imageDir = public_path('img/catalogue/products/' . $image_name);
        $imageDirSmall = public_path('img/catalogue/products/small_thumbs/' . $image_name);
        $imageDirMedium = public_path('img/catalogue/products/medium_thumbs/' . $image_name);
        $imageDirLarge = public_path('img/catalogue/products/large_thumbs/' . $image_name);
        /*
         * Save the Image and all of its resized siblings
         */     
        Image::make($image->getRealPath())->save($imageDir);    
        Image::make($image->getRealPath())->resize(null, 100, function ($constraint) {
                $constraint->aspectRatio();
            })->save($imageDirSmall);
        Image::make($image->getRealPath())->resize(null, 200, function ($constraint) {
                $constraint->aspectRatio();
            })->save($imageDirMedium);
        Image::make($image->getRealPath())->resize(null, 400, function ($constraint) {
                $constraint->aspectRatio();
            })->save($imageDirLarge);

        /*
         * Create a new entry in the database for the new product
         */
        $product = new Product;                       
        $product->description = $input['description'];    
        $product->content = $input['content'];      
        $product->price = $input['price'];      
        $product->product_code = $input['code'];    
        $product->main_image = $image_name;
        $product->status = 'enabled';
        $product->save();

        /*
         * Create the Relationship to its category
         */
        DB::table('category_product')->insert([
            'category_id' => $input['category_id'],
            'product_id' => $product->id
        ]);
    }
  }

代码中断第一个&#34; Image :: make ..&#34;函数运行所以我不确定这是因为我传递的数据不正确或是什么。

如果我var_dump一些东西,你可以看到输出:

$图片

object(Symfony\Component\HttpFoundation\File\UploadedFile)#27 (7) {
  ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  bool(false)
  ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  string(23) "22mm Sawn Whitewood.jpg"
  ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  string(24) "application/octet-stream"
  ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  int(0)
  ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
  int(1)
  ["pathName":"SplFileInfo":private]=>
  string(0) ""
  ["fileName":"SplFileInfo":private]=>
  string(0) ""
}

$ image-&gt; getRealPath()

string(52) "C:\Users\alex\Documents\websites\TottonTimber\public"

$ imageDir

string(109) "C:\Users\alex\Documents\websites\TottonTimber\public/img/catalogue/products/143869677322mm Sawn Whitewood.jpg"

我做错了什么?

0 个答案:

没有答案