[英]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.我可以通过 API 将图像上传到 WordPress 但不能上传音频文件,当我尝试将音频文件传递给该方法时,它会出现 HTTP 500 错误。 But when I pass the image it uploads successfully, here is the function.
但是当我通过它上传成功的图像时,这里是 function。
I'm using the Dio
package.我正在使用
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;
}
}
Got it.. added ".mp3" extension with filename variable and its perfect now明白了..添加了带有文件名变量的“.mp3”扩展名,现在完美了
FormData data = FormData.fromMap({
"file": await MultipartFile.fromFile(
file.path,
filename: fileName,
contentType: ...,//need to add this part
),
});
import 'package:mime/mime.dart';导入“包:mime/mime.dart”; //don't forget import
//不要忘记导入
var lookUpMime =lookupMimeType(file.path);
now need to split type现在需要拆分类型
var mimeString = mimeType.split('/')[0];
var mimeType = mimeType.split('/')[1];
and add it as contentType:并将其添加为 contentType:
contentType: MediaType(mimeString, mimeType),
I think it helps我觉得有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.