1

When i try to upload a csv file its MIME type changes to application/octet-stream. Although i specified enctype="multipart/form-data" in the form, how can i fix that?

<div class="container mt-5">
        <form action="{{route('fileUpload')}}" method="POST" enctype="multipart/form-data">
            <h3 class="text-center mb-5">Download file</h3>
            @csrf
            @if ($message = Session::get('success'))
                <div class="alert alert-success">
                    <strong>{{ $message }}</strong>
                </div>
            @endif

            @if (count($errors) > 0)
                <div class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <div class="custom-file">
                <input type="file" name="file" class="custom-file-input" id="chooseFile">
                <label class="custom-file-label" for="chooseFile">Choose file</label>
            </div>

            <button type="submit" name="submit" class="btn btn-primary btn-block mt-4">
                Upload
            </button>
        </form>
</div>
Abdo Rabah
  • 1,670
  • 2
  • 15
  • 31

1 Answers1

1

Recently I faced same problem - I'have trying to upload *.docx file to server using this code (on client):


function uploadFile(file) {

    const formData = new FormData();
    formData.append('file', file);

    const config = {
        headers: {
            'Content-Type': 'multipart/form-data',
            'Accept': 'application/json',
        }
    }

    return axios.post('/file/upload', formData, config);
}

server part:


// part from FileController.php 
private const ALLOWED_TYPES = [
        'image/png',
        'image/jpeg',
        'image/gif',
        'application/pdf',
        'application/msword',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    ];



// FileUploader.php
class FileUploader {

public static function uploadFile($file, $maxSize, $uploadPath, array $allowedTypes = []): string
    {
        if( !in_array($file->getMimeType(), $allowedTypes) ) {
            throw new \Exception("File type ({$file->getMimeType()}) not allowed." );
        }

        if( $file->getSize() > $maxSize ) {
            throw new \Exception('File too big.');
        }

        $name = $file->getClientOriginalName();
        $file->move( storage_path($uploadPath), $name );

        return $name;
    }
}

For most *.docx files uploading was successfull - it worked as expectet, but for certain sample.docx file - File type (application/octet-stream) not allowed. returned. And this is strange - only this file can't be uploaded.

The link that presented in previous comments, explain that this happens when server can't deterime file mime-type. So I fixed it by creating a new file with content of previous file - now it works.

Kiazim Khutaba
  • 191
  • 2
  • 14