首先,让我简单解释一下我想要实现的目标。
用户填写表格并在提交时我想使用 jQuerys $.post 方法发送所有数据,然后使用 Laravel 5.1 和干预 API(http://image.intervention.io/)我想处理数据并将图像上传到服务器上的正确目录。
我的数据确实发送到服务器(本地),我可以使用 Laravel 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
]);
}
}
代码在第一个“Image::make..”函数运行的地方中断,所以我不确定这是因为我传递了不正确的数据还是什么。
如果我 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->getRealPath()
string(52) "C:\Users\alex\Documents\websites\TottonTimber\public"
$图像目录
string(109) "C:\Users\alex\Documents\websites\TottonTimber\public/img/catalogue/products/143869677322mm Sawn Whitewood.jpg"
我做错了什么?