Skip to content

Blob ​

Blob stands for "Binary Large Object," and it is a data type used to store binary data such as images, audio files, or videos.

Blobs can be easily manipulated with QuickRequest, whether for upload or download.

Upload File ​

If you want to upload files through QuickRequest, use the POST method. It's important to define the form in your HTML with enctype="multipart/form-data".

  • Routes in web.php:
php
Route::post('/upload-image', [YourController::class, 'uploadImage']);
  • HTML Form: To create a file-upload form, structure it as follows:
html
<form id="uploadFile" enctype="multipart/form-data">
    <!-- ... -->
    <input type="file" id="image" name="image" accept=".jpg, .jpeg, .png">
    <!-- ... -->
</form>
  • QuickRequest Syntax: In this case, always use form as the data source for QuickRequest:
javascript
QuickRequest().post({
    url: '/upload-image',
    form: 'uploadFile',
    success: function(response, data, code){
        console.log("Successful Process");
    },
    error: function(response, data, code){
        console.error("Error: " + data.message);
    }
});

This way, all values from your form, including files, will reach the backend. Forget about creating FormData and other validations to upload a file.

Download File ​

If you want to download a file, you can easily do it using the GET method.

  • Routes in web.php:
php
Route::get('/image/{name}', [YourController::class, 'findImage']);
  • Method in YourController.php:
php
public function findImage($name)
{
    /**
     * Execute actions.
     * In this example, search for an image
     * in the public folder.
     */
    $pathToImage = public_path($name . ".jpeg");
    $imageContents = file_get_contents($pathToImage);

    /**
     * Return the image as a "Binary Large Object" (Blob).
     * Important to define ->header('Content-Type', ?)
     */
    return response($imageContents, 200)->header('Content-Type', 'image/jpeg');
}
  • QuickRequest Request:

Now that you are receiving an image as a "Binary Large Object," let's easily download it with QuickRequestBlobs:

javascript
const nameImage = 'LaravelLogo';

QuickRequest().get({
    url: '/image/' + nameImage,
    expect: 'blob', // Mandatory
    success: function (response, data, code) {
        /**
         * To download the file, simply
         * Use the QuickRequestBlobs object that facilitates
         * the action.
         */
        QuickRequestBlobs.setBlob(data)         // Always arrives at this position the blob
                         .setName(nameImage)    // Preferably without spaces.
                         .setExtension("jpeg")  // Lowercase extension without the dot.
                         .download();
    },
    error: function (response, data, code) {
        console.error("Error: " + data.message);
    }
});

Reminder

In controllers, always use the ->header('Content-Type', '?') method, where ? will be the corresponding value according to the type of file you want to download.

Here are some common headers:

File TypeContent-Type Header
JPEG ImageContent-Type: image/jpeg
PNG ImageContent-Type: image/png
GIF ImageContent-Type: image/gif
BMP ImageContent-Type: image/bmp
WebP ImageContent-Type: image/webp
SVG ImageContent-Type: image/svg+xml
PDF DocumentContent-Type: application/pdf
Excel Spreadsheet (XLSX)Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Word Document (DOCX)Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
PowerPoint (PPTX)Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation
CSV (Comma-Separated Values)Content-Type: text/csv
XMLContent-Type: application/xml
ZIP ArchiveContent-Type: application/zip

Released under the MIT License.