'How to upload Audio file to wordpress via Rest API from flutter

I am able to upload images via API to WordPress but not audio files, it gives HTTP 500 error when I try to pass the Audio file to the method. But when I pass the image it uploads successfully, here is the function.

I'm using the Dio package.


Future<bool> UploadFileToWordpress(File file) async {

    try {
      final token = MyConstant.token;

      String apiURL = "https://prostate-wrench.000webhostapp.com";
      String uri = "$apiURL/wp-json/wp/v2/media";

      String fileName = file.path
          .split('/')
          .last;
      print(fileName);

      FormData data = FormData.fromMap({
        "file": await MultipartFile.fromFile(
          file.path,
          filename: fileName,
        ),
      });

      Dio dio = Dio(BaseOptions(
          headers: {
            'Authorization': 'Bearer ${MyConstant.token}'
          },
          contentType: "application/json")
      );
      var response = await dio.post(uri, data: data);
      print(response.statusMessage);
      if (response.statusCode == 201) {
        return true;
      } else {
        return false;
      }

   
    }catch(e){
      print(e.toString());
      return false;
    }
  }



Solution 1:[1]

Got it.. added ".mp3" extension with filename variable and its perfect now

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdullah Butt