如何使用AngularJS上传音频/视频

时间:2016-06-27 08:59:05

标签: javascript jquery angularjs

我正致力于音频/视频上传和任务。有人可以帮我展示在Angular JS中实现这个功能吗?

1 个答案:

答案 0 :(得分:0)

我们需要更多详细信息,您尝试过的内容,您正在使用的后端类型(REST,SOAP,Web Service),使用哪种语言...

我使用Java RESTful服务器,可能不是更聪明的解决方案。

$scope.uploadVideo = function(){

    // Obtaining the selected file. In case of many, the others will be from the position 1 of the array
    var f = document.getElementById("videoInput").files[0];
    console.log("====================>SIZE: "+f.size);
    var name = f.name;

    //Getting the file name and its extension

    }

    //Loading the file and passing the callback as a parameter
    loadFile(f, function(data){
        // The file consist on its name and bytes
        var file={
            name:name,
            data:data
        }
        //Sending the video
        servicioRest.postFile(file)
        .then(function(){
            //Whatever

        })
        .catch(function(err){
            console.log("ERROR");
            alert("ERROR: "+err);
        });
    });        
};

//---------------------------------------------------- UTILS ----------------------------------------------

function loadFile(source, callBack){;
    var r = new FileReader();

    //Event that will be triggered when FileReader finish reading the file
    r.onloadend = function(e){
        var data = e.target.result;
        // some chars cannot be sent through http requests. So we encode them in base64 to ensure we send the correct bytes
        data=btoa(data);    
        console.log(data.length);
        callBack(data);
    //send you binary data via $http or $resource or do anything else with it
    }

    //Reading the file. When it ends, the event "onloadend", previously declared, will be triggered
    r.readAsBinaryString(source);
}

然后,在服务器中,我解码了字节并用它们和那些字节创建了一个文件......或者将它们保存在数据库中......无论你想要什么。

但我坚持认为,我们需要更多细节才能正确回答您。在我的名单上,仅使用该信息正确回答您是不可能的