PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 16, 2022

[FIXED] How to upload Audio file to wordpress via Rest API from flutter

 May 16, 2022     api, audio, flutter, wordpress, wordpress-rest-api     No comments   

Issue

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

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



Answered By - Abdullah Butt
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing