Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload bitmap data from flash to laravel route

I have a video player built in AS3. I take a snapshot of the video player using this code:

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);

I need to upload the encoded to JPG using one of my Laravel routes. My route looks like:

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

When I run the AS3 code, it calls the laravel route, but my $request variable appears to be empty. The Request Payload property on the network info tab that shows all my headers and stuff contains what looks like the source of the image file.

If I do a return Response::json(['filedata' => $request]); all I get is this:

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

My uploadFramegrab function is simply this for now:

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

I've searched online but I cannot find anything specifically for uploading from flash to laravel. I've done it javascript to laravel no problem. Anyone know what this could be? If you'd like more information please ask.

like image 689
Ronnie Avatar asked Sep 18 '15 18:09

Ronnie


1 Answers

To do that, you can use the Multipart.as ( AS3 multipart form data request generator ) from Jonas Monnier. It's really very easy to use it, take a look on this example ( using the basic example from the github project's page ) :

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);

Then, in the PHP side, you do as you have usually did :

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 !');
}

Hope that can help.

like image 55
akmozo Avatar answered Sep 27 '22 23:09

akmozo