I'm using zf2 rest for my api, and I'm using that to download mp3s. The api actually creates mp3s, then sends them to the client. I can only deduce that angular and my front-end code is creating this issue, because a "pure" URL entry into the browser works fine.
I suspect it's the frontend code creating the issue, because the file that the server creates is identical in either the "pure" URL or the angular request. When it gets to the client however, a 2MB file is 3.6MB, and doesn't play. Here's the relevant front end code from the angular controller.
$scope.createMp3 = function(list){
console.log(list);
var lessonList =list;
$http({
method:'POST',
url:'/api/lessons',
data:{cards:lessonList},
headers : {
'Content-type' : 'audio/mpeg',
},
}).success(function(data, status, headers, config){
// console.log(data);
var file = new Blob([ data ]);
//trick to download store a file having its URL
var fileURL = URL.createObjectURL(file);
var a = document.createElement('a');
a.text = "Download Lesson"
a.href = fileURL;
a.target = '_blank';
a.download = 'yourfilename.mp3';
document.body.appendChild(a);
a.click();
}).error(function( e){
console.log('error', e);
});
};
from this post how to download file using AngularJS and calling MVC API?
For testing, the server isn't dynamically responding to "lessonList". It's creating the same mp3 every time, and hashes of the created file confirm that there are no discrepancies.
What I've tried: Changing the Content-type header to null. By that I mean not including it in the $http config object.
I can't think of anything else to try. This works fine when entering "www.example.com/api/lessons" into the browser as a GET, and the GET and POST methods are identical on the back end, so I think it has to be the front end code. The hashes are identical for the "pure" GET received file and the backend-generated file. They are not identical using this method in angular.
EDIT: When using SoX to play the downloaded mp3, I get the following error:"play WARN mp3-util: MAD lost sync" multiple times. Again, it doesn't occur using the browser URL method.
Any suggestions?