How to Download and Upload Product Attachment Files in Magento 2?

11 Min | 20 July, 2023

While the default Magento page can display all kinds of product information, there are cases where you might want to provide more information (for example, user manuals, technical specs, etc.).

Consequently, it becomes crucial for Magento stores to link files to such product pages. This can be accomplished by displaying an appealing icon and an informative document, capturing guests’ attention and encouraging them to proceed with the checkout process.

The Magento admin is responsible for providing additional user instructions, documentation, photos, and videos related to specific products.

However, by default, Magento does not permit customers to upload files from the backend. Instead, customers can swiftly download associated Magento 2 product attachments from the front end. Furthermore, keeping your product content concise and attaching relevant files allows consumers to access all the necessary information in one place and download it if needed.

Various methods exist to upload product attachments for your customers, such as utilizing Magento Extensions or programmatically uploading files in Magento 2 through system configuration.

If you want to learn about creating products, then check this guide on how to manage products in Magento.

Adding Magento 2 Product Attachments Using Extensions

Configure Product Attachments in Magento 2 using these three extensions:

Magento 2 Product Attachments by MageComp

Magento 2 Product Attachments by MageComp

When selling compound products, it is common for your shop visitors to seek comprehensive information that can help them make informed purchasing decisions. Clients may require manuals or attachments that cannot be adequately described in the product description.

MageComp’s Product Attachment Extension for Magento 2 stores allows store owners to upload product attachments displayed on a separate tab in the front end. With this extension, the admin can modify the title of the attachment tab in the front end and upload files to the Magento 2 store.

Features

  • Separate tab display for attachments.
  • Ability to upload attachments using the backend admin area.
  • Backend configuration for managing allowed file type extensions.
  • Support PDFs, Excel, documents, images, and various media types.
  • Compatibility with Magento versions 2.1, 2.2, 2.3, and 2.4.

Price

  • The Product Attachment Extension is available for $49.
  • It comes with a 30-day money-back guarantee.
  • Lifetime free upgrades are included.
  • Users will also receive 90 days of free support.

Configuration

  • After installing the extension, navigate to Stores > Configuration to access the settings.
  • Under the Product Attachments option, you can turn the extension on or off.
  • Select “YES” for “Show Attachments On Product Detail” to display attachments on the front end.
  • Similarly, select “YES” for “Show on Product Tab” to display the attachments on the product tab.

Magento Product Attachments

  • In the Manage Product Attachments section, you can add/create new attachments or view existing ones.

Magento 2 Product Attachments - Add New

  • To add a new attachment, click “Add New” and provide the relevant details, as shown in the screenshot below. Ensure the status is set to “YES” and click “Save and Continue Edit” to display the attachments to your visitors.

Magento 2 Product Attachments - Setting Up New Attachment

  • To enable the attachment for a specific product, click on the “Select Products” option and save your changes.

Magento 2 Product Attachments - Select Product for Attachment

  • The uploaded file in the Magento 2 store can be seen as depicted below, and customers can also download the attachment.

Magento 2 Product Attachments - Storefront View

That’s all you need to do!

Product Attachment Extension for Magento 2 by Mconnect

Product Attachment Extension for Magento 2 by Mconnect

The Product Attachment Extension for Magento 2 by Mconnect Media simplifies the process of uploading and downloading attachments for Magento store owners.

This extension enables you to provide yours customers with additional information about your products/services through downloadable file attachments. It allows you to display user guides, tutorials, privacy policies, and other relevant materials on the front end of your Magento 2 store.

Features

  • Attachments are displayed in a separate tab-based list view on the front end.
  • Backend grid for efficient management of uploaded files.
  • Auto-detection of file types with corresponding icons, titles, and sizes.
  • Each product can have an unlimited number of attachments.
  • Ability to upload any file type and assign it to specific products using the grid.
  • AJAX technology enables the uploading of multiple files without page reload.
  • Customization of file visibility based on specified shop views and customer groups.
  • Provision of additional information to customers through videos, tutorials, photos, documents, PowerPoint presentations, and more.
  • Magento Community Versions: 2.0.x, 2.1.x, 2.2.x, 2.3.x, 2.4.x.

Price

  • The Product Attachment Extension is priced at $79.
  • It comes with a 30-day money-back guarantee.
  • Lifetime free upgrades are included.
  • Users will receive 90 days of free support.

Configuration

  • Login to the admin panel of your Magento 2 store and navigate to Store > Configuration > M-Connect > Product File Upload.
  • Enter the License and Serial Key provided in the zip file.

Product Attachment Extension for Magento 2 by Mconnect Licence

  • Go to General Settings.
  • Choose “Yes” or “No” to turn the extension on or off.
  • Specify the allowable file extensions in the “Allowable File Extensions” field.
  • Select “Yes” or “No” to allow or deny file downloads for non-logged-in users.
  • Set the “Redirect to Referrer Page after login” option to “Yes” or “No” to determine whether users will be redirected to the previous page after successful login.
  • Enter custom text for a successful login.

Product Attachment Extension for Magento 2 by Mconnect - General Setting

  • Go to M-Connect Media in the left category and select “Manage Product File Upload.”
  • Click the “Add New” button and open a new window.

Product Attachment Extension for Magento 2 by Mconnect - Product File Upload

  • In the General Information section, enter the file name, add the product image, and set the status to “Enable” to display the product image.

Product Attachment Extension for Magento 2 by Mconnect - Product File Upload Information

  • In Store Views, select the store from the drop-down where you want to display the image and downloadable file.

Product Attachment Extension for Magento 2 by Mconnect - Store Views

  • In the Products section, you can choose the number of products you want to assign the file simultaneously. Then click on “Save.”

Product Attachment Extension for Magento 2 - Final Steps

Product Attachments for Magento 2 by MagePlaza

Product Attachments for Magento 2 by MagePlaza

Mageplaza is a Magento 2 extensions provider site, it offers product attachment extension that enhances the functionality of product pages in e-commerce stores. This extension enables online business owners to attach various files to their product pages, providing valuable information and resources to customers.

The Mageplaza Product Attachment extension features include multiple file formats, easy management, customizable icons, customer permissions, frontend display, SEO-friendly content, and improved user experience. By offering relevant and valuable files for download, Magento 2 store owners can enhance the shopping experience for their customer and build trust with customers, and facilitate informed purchase decisions.

Features

  • Ability to attach single or multiple files to a product.
  • An unlimited number of attachments.
  • Drag and drop feature for easy file upload.
  • Option to add multiple CSV files.
  • Configuration of file visibility on specific store views.
  • Ability to hide attachments from non-login customers.
  • Display attachments in a separate tab-based list view on the front end.
  • Ability to attach any URL on the products, such as YouTube, Dropbox, Google Drive, or any other link.
  • Provision of 40 icon packages for automatic identification of attachment formats.
  • Compatible with CE/EE/B2B/Cloud/Adobe Commerce 2.2.x, 2.3.x, 2.4.x versions.

Price

  • The cost of Product Attachments for Magento 2 is $129.
  • 60-day money-back guarantee.
  • Free upgrades for a lifetime.
  • Receive 365 days of support.

Configuration

To configure Product Attachments for Magento 2, follow these steps:

  • Log in to the Magento 2 admin panel and navigate to Catalog > Product Attachments > Configuration.

Product Attachments for Magento 2 by MagePlaza

On the next page, follow the instructions below:

GENERAL SETTINGS

  • Enable: Select “Yes.”
  • Block Title: Enter the title of the attachment.
  • Show On: Choose the positions to display Product Attachments, such as in the Product Tabs, under the Add to Cart button, Sidebar, or all.
  • Display Attachment File Size: Optional setting to display the file size.
  • File Size Limit Allowed (MB): Attachment files must be below 25MB to be sent via Gmail.
  • Add Attachments in Order Emails: Select “Yes” to include attachments in order emails.

Product Attachments for Magento 2 by MagePlaza

  • Groups: Attachments are displayed on the front end in a separate tab-based list view.

Product Attachments for Magento 2 by MagePlaza

The settings selected in this area serve as the default value when uploading a product attachment. These settings are initially applied when adding new files, but the admin can modify them when editing or adding new files.

Product Attachments for Magento 2 by MagePlaza

MANAGE FILES

Navigate to Catalog > Product Attachments > Manage Files.

  • Admins can view essential information about each file in the grid, including the ID, Label, Name, Status, Store View, Customer Group, Group, Priority, Create Date, and Action.
  • Admins can perform actions such as deletion by selecting the file and choosing “Action/Delete” in the Action Dialog. A confirmation pop-up will appear, and clicking “OK” will proceed with the deletion.
  • To update file information, admins can click “Edit” in the Action column.
  • Additionally, admins can filter, change the store view, show/hide columns, and export the grid.

Product Attachments for Magento 2 by MagePlaza - File Manager

Add New/Edit Form

Product Attachments for Magento 2 by MagePlaza

Download and Upload files in Magento 2 by System Configuration

Download and upload files in Magento 2 programmatically by system configuration.

Magento 2 File Upload Programmatically

To provide users with a comprehensive understanding of a product’s appearance and functionality, it is necessary to include multiple photos and files, such as product images from different angles, videos, user guides, size charts, tutorials, privacy policies, and terms of service, among others.

You have two options for uploading files in Magento 2: use the previously mentioned extensions or utilize the code below within a custom module to programmatically upload files.

There are two methods for programmatically uploading files:

  • Create an input file field.
  • The controller will receive the request and save the files.

Below is the HTML code for the input file field:

<input type="file" name="upload_custom_file" id="upload_custom_file" title="Upload Custom File" data-validate="{required:true}">

And here is an example of the controller class file code:

<?php
namespace VendorNameModuleNameController;

use MagentoFrameworkAppActionContext;
use MagentoFrameworkMessageManagerInterface;
use MagentoFrameworkFilesystem;
use MagentoMediaStorageModelFileUploaderFactory;
class YourController extends MagentoFrameworkAppActionAction
{

protected $messageManager;
protected $filesystem;
protected $fileUploader;

public function __construct(
Context $context,
ManagerInterface $messageManager,

Filesystem $filesystem,
UploaderFactory $fileUploader
)
{
$this->messageManager = $messageManager;

$this->filesystem = $filesystem;
$this->fileUploader = $fileUploader;

$this->mediaDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);

parent::__construct($context);
}

public function execute()
{
// your code

$uploadedFile = $this->uploadFile();

// your code
}

public function uploadFile()
{
// this folder will be created inside the “pub/media” folder
$yourFolderName = ‘your-custom-folder/’;

// “upload_custom_file” is the HTML input file name
$yourInputFileName = ‘upload_custom_file’;

try{
$file = $this->getRequest()->getFiles($yourInputFileName);
$fileName = ($file && array_key_exists(‘name’, $file)) ? $file[‘name’] : null;

if ($file && $fileName) {
$target = $this->mediaDirectory->getAbsolutePath($yourFolderName);

/** @var $uploader MagentoMediaStorageModelFileUploader */
$uploader = $this->fileUploader->create([‘fileId’ => $yourInputFileName]);

//Set allowed file extensions
$uploader->setAllowedExtensions([‘jpg’, ‘pdf’, ‘doc’, ‘png’, ‘zip’]);

// allow folder creation
$uploader->setAllowCreateFolders(true);

// rename file name if it already exists
$uploader->setAllowRenameFiles(true);

//Upload the file to the specified folder
$result = $uploader->save($target);

//echo ‘<pre>’; print_r($result); exit;

if ($result[‘file’]) {
$this->messageManager->addSuccess(__(‘File has been successfully uploaded.’));
}

return $target . $uploader->getUploadedFileName();
}
} catch (Exception $e) {
$this->messageManager->addError($e->getMessage());
}

return false;
}
}

These steps provide a straightforward approach to programmatically uploading files in Magento 2.

Magento 2 File Download Programmatically

Various file types are supported, including CSV files, PNG/JPG images, videos, and PDF files. You can download the file at any time if it is permitted. You can utilize the provided code if you want to apply the same functionality to a personalized page.

Copy and paste it into your controller below the existing code.

<?php

namespace MageDelightHelloworldControllerIndex;

use MagentoFrameworkAppFilesystemDirectoryList;

class Index extends MagentoFrameworkAppActionAction
{
/**
* @var MagentoFrameworkAppResponseHttpFileFactory
*/
protected $fileFactory;

/**
* @param MagentoFrameworkAppActionContext $context
* @param MagentoFrameworkAppResponseHttpFileFactory $fileFactory
*/
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkAppResponseHttpFileFactory $fileFactory
) {
$this->fileFactory = $fileFactory;
parent::__construct($context);
}

public function execute()
{
$filePath = ‘media/md_product_print/logo.svg’;
$downloadName = ‘logo1.svg’;
$content[‘type’] = ‘filename’;
$content[‘value’] = $filePath;
$content[‘rm’] = 0; // If you will set here 1 then, it will remove the file from the location.
return $this->fileFactory->create($downloadName, $content, DirectoryList::PUB);
}
}

Inject the following class: MagentoFrameworkAppResponseHttpFileFactory into your file and add the execute() method within the method’s logic.

In the create() method, the first argument should be the desired file name for downloading, the second argument should be an output array for the file, and the third argument should be the directory path from which the download file is desired. For the file path, add the pub/media image path here. You can use DirectoryList::PUB to retrieve the file from the pub directory as the directory path.

Remember to clean the cache before proceeding with the action.

Conclusion

In conclusion, downloading and uploading files or configuring Magento 2 product attachments can be done through two main methods: using extensions or configuring the system settings programmatically.

By combining the power of extensions and programmatically configuring system settings, Magento 2 provides users a versatile and efficient solution for downloading and uploading files. Whether users prefer ready-made extensions or a more customized approach, Magento 2 offers a robust platform for handling files in an e-commerce environment.

If you are looking for a fast and secure cloud hosting platform for your store, check out Devrims Managed Magento Hosting.

The Author

Anmol is a Digital Content Producer at Devrims. She loves to write technical tutorials about Python, WordPress, PHP, and Magento. Also, she likes to play table tennis with friends in her free time.

Scroll to Top