2016-06-09 75 views
0

我目前正试图让文件上传工作,但遇到了一个问题,因为表单正在使用多部分表单数据而不是JSON对象上传有效内容。返回&文件上传支持 - JSON

返回&将只接受文件名和FILEDATA JSON对象里面,但我无法弄清楚如何与NG管理员做到这一点。

我的代码目前看起来是这样的:

.uploadInformation({ 'url': BackandProvider.getApiUrl()+'/1/objects/action/games', 'params': {'name':'files'}, 'headers': { 'Content-Type': false }, 'data': data }) 
+0

这NG管理员您使用,也许这一个:https://github.com/marmelab/ng-admin支持JSON – user3375230

回答

0

我看你使用BackandProvider所以绕过这一点,你可以实现自己上传。 从Backand docs

<body class="container" ng-app="app" ng-controller="DemoCtrl" ng-init="initCtrl()"> 
    <h2>Backand Simple Upload File</h2> 
    <br/> 
    <form role="form" name="uploadForm"> 
    <div class="row"> 
     <img ng-src="" ng-show="imageUrl" /> 
     <input id="fileInput" type="file" accept="*/*" ng-model="filename" /> 
     <input type="button" value="x" class="delete-file" title="Delete file" ng-disabled="!imageUrl" ng-click="deleteFile()" /> 

    </div> 
    </form> 
</body> 




    // input file onchange callback 
    function imageChanged(fileInput) { 

    //read file content 
    var file = fileInput.files[0]; 
    var reader = new FileReader(); 

    reader.onload = function(e) { 
     upload(file.name, e.currentTarget.result).then(function(res) { 
     $scope.imageUrl = res.data.url; 
     $scope.filename = file.name; 
     }, function(err){ 
     alert(err.data); 
     }); 
    }; 

    reader.readAsDataURL(file); 
    }; 

    // register to change event on input file 
    function initUpload() { 
    var fileInput = document.getElementById('fileInput'); 

    fileInput.addEventListener('change', function(e) { 
     imageChanged(fileInput); 
    }); 
    } 

    // call to Backand action with the file name and file data 
    function upload(filename, filedata) { 
    // By calling the files action with POST method in will perform 
    // an upload of the file into Backand Storage 
    return $http({ 
     method: 'POST', 
     url : Backand.getApiUrl() + baseActionUrl + objectName, 
     params:{ 
     "name": filesActionName 
     }, 
     headers: { 
     'Content-Type': 'application/json' 
     }, 
     // you need to provide the file name and the file data 
     data: { 
     "filename": filename, 
     "filedata": filedata.substr(filedata.indexOf(',') + 1, filedata.length) //need to remove the file prefix type 
     } 
    }); 
    };