将位图数据从闪存上传到 laravel 路由

Upload bitmap data from flash to laravel route

我有一个内置 AS3 的视频播放器。我使用此代码拍摄视频播放器的快照:

var uploadUrl = 'http://localhost:8000/assets/uploadframegrab';
var bitmap = new Bitmap();        
var graphicsData : Vector.<IGraphicsData>;
graphicsData = container.graphics.readGraphicsData();
bitmap.bitmapData = GraphicsBitmapFill(graphicsData[0]).bitmapData;

var jpgEncoder:JPGEncoder = new JPGEncoder(85);
var jpgStream:ByteArray = jpgEncoder.encode(bitmap.bitmapData);

var loader:URLLoader = new URLLoader();
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var csrf:URLRequestHeader = new URLRequestHeader("X-CSRF-Token", csrfToken);        
var request:URLRequest = new URLRequest(uploadUrl);
request.requestHeaders.push(header);
request.requestHeaders.push(csrf);
request.method = URLRequestMethod.POST;
request.data = jpgStream;
loader.load(request);

我需要使用我的 Laravel 路线之一将编码上传到 JPG。我的路线如下:

Route::post('assets/uploadframegrab', 'AssetController@uploadFramegrab');

当我 运行 AS3 代码时,它调用 laravel 路由,但我的 $request 变量似乎是空的。网络信息选项卡上的 Request Payload 属性 显示了我的所有 headers 和内容,其中包含看起来像图像文件源的内容。

如果我做 return Response::json(['filedata' => $request]); 我得到的是:

filedata: {
  attributes: {},
  request: {},
  query: {},
  server: {},
  files: {},
  cookies: {},
  headers: {}
}

我的 uploadFramegrab 功能现在很简单:

public function uploadFramegrab(Request $request)
{
  if ($request)
  {
    return Response::json(['filedata' => $request]);
  }
  else
  {
    return Response::json(['error' => 'no file uploaded']);
  }
}

我在网上搜索过,但找不到任何专门用于从 Flash 上传到 laravel 的内容。我做到了 javascript 到 laravel 没问题。有人知道这可能是什么吗?如果您想了解更多信息,请询问。

基于 AS3 的 doc(强调我的):

The way in which the data is used depends on the type of object used:

  • If the object is a ByteArray object, the binary data of the ByteArray object is used as POST data. For GET, data of ByteArray type is not supported. Also, data of ByteArray type is not supported for FileReference.upload() and FileReference.download().
  • If the object is a URLVariables object and the method is POST, the variables are encoded using x-www-form-urlencoded format and the resulting string is used as POST data. An exception is a call to FileReference.upload(), in which the variables are sent as separate fields in a multipart/form-data post.

你显然属于第一种情况。

来自Laravel Requests doc

To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller constructor or method. The current request instance will automatically be injected by the service container.

Request class API

string|resource getContent(bool $asResource = false)

Returns the request body content.

放在一起:

public function uploadFramegrab(Request $request) {
    $content = $request->getContent();
    $fileSize = strlen($content);
}

在Laravel 4中:

$csrf = Request::header('X-CSRF-Token');  
// Add a header like this if you want to control filename from AS3
$fileName = Request::header('X-File-Name');  
$content = Request::getContent(); // This the raw JPG byte array
$fileSize = strlen($content);

我上次检查 Laravel 使用 php://input 读取请求正文。请参阅此 answer 了解更多信息。

为此,您可以使用 Multipart.as ( AS3 multipart form data request generator ) from Jonas Monnier。它真的很容易使用,看看这个例子(使用 github 项目页面的基本例子):

var upload_url:String = 'http://www.example.com/upload';

// create an orange square
var bmp_data:BitmapData = new BitmapData(400, 400, false, 0xff9900);

// compress our BitmapData as a jpg image   
var image:ByteArray = new JPGEncoder(75).encode(bmp_data);

// create our Multipart form
var form:Multipart = new Multipart(upload_url);

    // add some fields if you need to send some informations
    form.addField('name', 'bmp.jpg');
    form.addField('size', image.length.toString());

    // add our image
    form.addFile('image', image, 'image/jpeg', 'bmp.jpg');

var loader:URLLoader = new URLLoader();
    loader.load(form.request);

然后,在PHP这边,你照常做:

public function upload(\Illuminate\Http\Request $request)
{        
    if($request->hasFile('image'))
    {            
        $file = $request->file('image');            
        $upload_success = $file->move($your_upload_dir, $file->getClientOriginalName());

        if($upload_success)
        {
            return('The file "'.$request->get('name').'" was successfully uploaded');
        } 
        else 
        {
            return('An error has occurred !');
        }

    }        
    return('There is no "image" file !');
}

希望能帮到你。