简体   繁体   English

AngularJS-如何通过$ http发布发送音频文件?

[英]AngularJS - How to send an audio file through $http post?

So I've been trying to send an audio file through an $http service using FormData, and so far what I have tried to send the file hasn't worked yet. 因此,我一直在尝试使用FormData通过$ http服务发送音频文件,到目前为止,我尝试发送的文件还没有成功。

This is how the service looks like: 服务的外观如下:

songs_services.add_new_song = function(new_song_name, new_song_artist, song) {
    var fd = new FormData();

    fd.append("new_song_name", new_song_name);
    fd.append("new_song_artist", new_song_artist);
    fd.append("song", song);

    console.log(fd.get("new_song_name"));
    console.log(fd.get("new_song_artist"));
    console.log(fd.get("song"));

    return $http.post(BACKEND_PREFIX + "add_new_song", fd, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
    }).then(function() {

    }, function() {

    });
};

I wanted to make sure that the information was actually been appended to my FormData and this is what i get in the console: 我想确保信息实际上已附加到我的FormData中,这就是我在控制台中得到的信息:

在此处输入图片说明

So now I know that the FormData has actually the information that I need. 所以现在我知道FormData实际上具有我需要的信息。

I have also tried changing the Content-Type to multipart/form-data with no success also. 我也曾尝试将Content-Type更改为multipart / form-data,但也没有成功。

I'm also using CakePHP 2 as my backend, so this is how I'm trying to get the information: 我还使用CakePHP 2作为后端,因此这就是我尝试获取信息的方式:

public function add_new_song() {
    $this->autoRender = false;

    $data = json_decode(file_get_contents("php://input"));

    print_r($data);
    print_r($_POST);
    print_r($_FILES);

    $new_song_name = $_POST["new_song_name"];
    $new_song_artist = $_POST["new_song_artist"];
    $song = $_FILES;

    echo $new_song_name;
    echo "<br />";
    echo $new_song_artist;
    echo "<br />";
    print_r($song);

    die();
}

But echoing the variables only shows empty arrays and I also get an undefined index error when trying to access the variables from $_POST. 但是回显变量只会显示为空数组,并且尝试从$ _POST访问变量时也会出现未定义的索引错误。

Is there any special way I should be sending the audio file through $http? 我应该通过$ http有什么特殊的方式发送音频文件吗? I really feel like I'm missing a little detail. 我真的觉得我缺少一些细节。

At last, instead of using angularjs $http.post I decided to try with $.ajax and see what happened, and it actually worked! 最后,我决定不尝试使用$ .ajax,而是尝试使用$ .ajax来查看发生了什么,并且实际上起作用了,而不是使用angularjs $ http.post。

Here's what I used: 这是我使用的:

$.ajax({
    type : "post",
    url : "uploads/songs",
    data : fd,
    cache : false,
    contentType : false,
    processData : false,
    success: function() {
        console.log("Hey");
    }
});

And in Angular 6, you can do the following to send audio as FormData. 在Angular 6中,您可以执行以下操作以将音频作为FormData发送。

Inject the HttpClient: 注入HttpClient:

constructor(private http:HttpClient){}

Use the POST method to send audio: 使用POST方法发送音频:

let url = 'http://destinationurl.com/endpoint';
let formData = new FormData();
formData.append('myAudioFile',audioFile);
this.http.post(url,formData).subscribe(response => {
    //handle response
}, err => {
    //handle error
});

the post method will automatically change the content type to multipart, so you need not set anything manually. post方法将自动将内容类型更改为多部分内容,因此您无需手动设置任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM