How to send a file via Axios to Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Send Files via Axios to Laravel: Mastering Multipart Form Data
Sending files from a frontend application (like Vue.js) to a backend framework (like Laravel) is one of the most common, yet often misunderstood, tasks in web development. When dealing with file uploads, developers frequently run into issues where the server simply doesn't recognize the incoming data, leading to errors like "No! It's not a File."
As a senior developer, I can tell you that this issue almost always stems from an incorrect configuration of how the data is packaged and sent over HTTP. The key lies in understanding the difference between sending JSON data (which uses application/json) and sending file data (which requires multipart/form-data).
This guide will walk you through the correct, robust way to handle file uploads using Axios and Laravel, ensuring your data flows smoothly from client to server.
The Core Problem: Misunderstanding multipart/form-data
Your initial approach was close, but sending a raw JavaScript File object directly in the request body via Axios doesn't automatically format it correctly for a file upload. To successfully send files, you must package the file data along with any other form fields into a FormData object. This object is specifically designed to create the necessary multipart/form-data encoding that web servers expect when receiving file uploads.
Step 1: The Vue/Axios Client Side (Correct Implementation)
To correctly send a file, you must use the built-in JavaScript FormData interface. This object allows you to construct a set of key/value pairs representing form fields, including files, which Axios will then serialize into the correct format for the server.
Here is the corrected Vue.js implementation:
methods: {
'successUpload': function (file) {
// 1. Create a FormData object
const formData = new FormData();
// 2. Append the file using a specific field name (e.g., 'file')
formData.append('file', file);
// Note: When using FormData, you generally DO NOT set the Content-Type header manually.
// Axios handles setting the correct boundary automatically for multipart/form-data.
axios.post('/Upload/File', formData)
.then(function (response) {
console.log('File uploaded successfully:', response.data);
})
.catch(function (error) {
console.error('Error uploading file:', error);
});
}
}
Why this works: By using formData.append('file', file), you are telling the browser to package the file data under the field name file. This structure is exactly what Laravel's request handling expects when processing a multipart form submission.
Step 2: The Laravel Server Side (Receiving and Storing)
On the Laravel side, because you are sending multipart/form-data, you no longer need to manually set headers for this specific request type. Laravel's Request object automatically parses the incoming file data into the file() helper or the request body.
Here is how you should handle the upload in your controller:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
public function uploadFile(Request $request)
{
// Check if the file exists in the request
if ($request->hasFile('file')) {
// Get the uploaded file instance
$file = $request->file('file');
if ($file) {
// Define where the file will be stored (e.g., 'public/uploads')
$path = $file->store('public/uploads');
return response()->json(['message' => 'File uploaded successfully', 'path' => $path], 200);
}
}
// If the file is missing or an error occurred
return response()->json(['error' => 'No valid file was provided.'], 400);
}
Key Laravel Takeaways:
$request->file('field_name'): This method correctly retrieves the uploaded file object when the request ismultipart.- Storage Abstraction: For production applications, storing files directly on the local disk might be limiting. It is best practice to use Laravel's Storage facade (like
Storage::disk('public')->putFile(...)) or cloud storage services like AWS S3 for better scalability and security. This approach aligns perfectly with modern architectural patterns found in the Laravel ecosystem, making complex operations much cleaner.
Conclusion
The failure you experienced was due to a mismatch between how the client packaged the data and how the server expected it. By mastering the use of FormData on the client side and correctly utilizing $request->file() on the server side, you establish a reliable pipeline for file transfers. Remember that understanding these fundamental HTTP request types is crucial when building robust applications with frameworks like Laravel. Focus on structuring your data correctly at the source, and the rest of the interaction becomes straightforward and predictable.