Load raster data from flash memory into a laravel route

I have a video player built into AS3. I am taking a snapshot of a 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 load encoding into JPG using one of my Laravel routes. My route is as follows:

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

When I run the AS3 code, it calls the laravel route, but my $request variable seems empty. The Request Payload property on the network information tab, which displays all my headers and more, contains what looks like the source of the image file.

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

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

My uploadFramegrab function is just like this:

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

I searched on the Internet, but I can not find anything specifically for downloading from a flash file in laravel. I made this javascript for laravel without any problems. Does anyone know what this could be? If you need more information, please ask.

+6
source share
2 answers

You can use Multipart.as (a data request generator with multiple AS3 forms) from Jonas Monnier for this . It is very easy to use, look at this example (using the main example from the github project 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, on the PHP side, you do as usual:

 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 this helps.

+2
source

Based on the doc for AS3 (emphasis mine):

How 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 type ByteArray is not supported. In addition, data of type ByteArray is not supported for FileReference.upload () and FileReference.download ().
  • If the object is a URLVariables object and the POST method, the variables are encoded using the x-www-form-urlencoded format, and the resulting string is used as POST data. An exception is the call to FileReference.upload (), in which variables are sent as separate fields in the multipart / form-data message.

You are clearly in the first case here.

From the Laravel Requests doc :

To get an instance of the current HTTP request through dependency injection, you must inherit the Illuminate \ Http \ Request class from your constructor or controller method. The current request instance will be automatically entered by the service container.

API class request :

string | getContent resource (bool $ asResource = false)

Returns the contents of the request body.

Combining this:

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

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

The last time I checked, Laravel uses php://input to read the request body. See more details.

0
source

All Articles