2016-06-09 103 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 
     } 
    }); 
    };