File Upload using AngularJS

前端 未结 29 1997
野趣味
野趣味 2020-11-21 07:24

Here is my HTML form:

29条回答
  •  野的像风
    2020-11-21 08:05

    I've read all the thread and the HTML5 API solution looked the best. But it changes my binary files, corrupting them in a manner I've not investigated. The solution that worked perfectly for me was :

    HTML :

    
    
    

    JS:

    msds_update = function() {
        var f = document.getElementById('msds').files[0],
            r = new FileReader();
        r.onloadend = function(e) {
            var data = e.target.result;
            console.log(data);
            var fd = new FormData();
            fd.append('file', data);
            fd.append('file_name', f.name);
            $http.post('server_handler.php', fd, {
                transformRequest: angular.identity,
                headers: {'Content-Type': undefined}
            })
            .success(function(){
                console.log('success');
            })
            .error(function(){
                console.log('error');
            });
        };
        r.readAsDataURL(f);
    }
    

    Server side (PHP):

    $file_content = $_POST['file'];
    $file_content = substr($file_content,
        strlen('data:text/plain;base64,'));
    $file_content = base64_decode($file_content);
    

提交回复
热议问题