skip to Main Content

I’m developing my own API to receive files and using Flutter to create the app.

So I’m sending files to and API, using Flutter Dio, and I keep getting error 406.

'file': await MultipartFile.fromFile(file.path),

Turns out, I found out if I add the following line, it works:

'file_tar': await MultipartFile.fromFile(file.path, contentType: MediaType('application', 'x-tar')),

To work, both lines must be on the code.

On PHP web, the same thing, but I couldn’t find a workaround.

Funny thing, using the same API on Postman, and over there it doesn’t require this line, so I guess this might be Flutter Dio encoding the file somehow, requiring the x-tar Content-Type for the file.

Here is the entire Flutter code:

var dio = Dio();
var formData = FormData.fromMap({
  'file': await MultipartFile.fromFile(file.path),
  'file_tar': await MultipartFile.fromFile(file.path, contentType: MediaType('application', 'x-tar')),
});
var response = await dio.post(url,
    data: formData,
    options: Options(headers: {
      'Content-Type': 'multipart/form-data; charset=utf-8',
      'Accept': '*/*',
      'Connection': 'keep-alive',
      'Accept-Encoding': 'gzip, deflate, br',
    }), onSendProgress: (int sent, int total) {
  var progress = (sent / total) * 100;
  log('Progress: $progress %');
});

Thanks!!!

2

Answers


  1. You can try this way also. It might help you.

    var dio = Dio();
    var formData = FormData();
    
     formData.files.addAll([
      MapEntry("file", await MultipartFile.fromFile(file)),
       MapEntry("file_tar", await MultipartFile.fromFile(file ,contentType: MediaType('application', 'x-tar') )),
    ]);
    
    var response = await dio.post(url,
        data: formData,
        options: Options(headers: {
          'Content-Type': 'multipart/form-data; charset=utf-8',
          'Accept': '*/*',
          'Connection': 'keep-alive',
          'Accept-Encoding': 'gzip, deflate, br',
        }), onSendProgress: (int sent, int total) {
      var progress = (sent / total) * 100;
      log('Progress: $progress %');
    });
    

    OR

    formData.files.addAll([
      MapEntry(
       'file',
        MultipartFile.fromFileSync(file.path,filename: 'upload.test'),
      ),
      MapEntry(
        'file_tar',
        MultipartFile.fromFileSync(file.path,filename: 'upload.test'),
      ),
    ]);
    

    file_tar is your key then its fine. otherwise you can just write files as in the end its creates array of files.

    Login or Signup to reply.
  2. Might be content-type issue. Try this code:

    import 'dart:io';
    import 'package:dio/dio.dart';
    import 'package:http_parser/http_parser.dart';
    
    // ...
    
    var dio = Dio();
    var formData = FormData.fromMap({
      'file': await MultipartFile.fromFile(
        file.path,
        contentType: MediaType('application', 'octet-stream'),
      ),
    });
    var response = await dio.post(url,
        data: formData,
        options: Options(headers: {
          'Content-Type': 'multipart/form-data; charset=utf-8',
          'Accept': '*/*',
          'Connection': 'keep-alive',
          'Accept-Encoding': 'gzip, deflate, br',
        }), onSendProgress: (int sent, int total) {
      var progress = (sent / total) * 100;
      print('Progress: $progress %');
    });
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search