使用C#进行RESTful angularjs图像发布

时间:2015-04-05 16:04:18

标签: c# asp.net angularjs rest

我是AngularJs和Asp.Net Restful服务的新手。很长一段时间,我试图将图像数据发布到我的网络服务,但我无法成功。这是我的代码,我做错了什么?

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="../client/js/angular/data/angular.min.js"></script>
    <script src="../client/js/angular/data/angular-resource.js"></script>
    <script src="../client/js/angular/data/angular-files-model.min.js"></script>
    <title>Image Upload</title>
</head>
<body ng-app="fuApp">
    <div ng-controller="fuAppCtr">
        <table border="1">
            <tr>
                <td><input type="file" ng-files-model="fileModel"></td>
                <td>{{ fileMessage }}</td>
            </tr>
            <tr>
                <td><button ng-click="submit()">Save</button></td>
                <td></td>
            </tr>
        </table>
    </div>
    <script>
        var fuApp = angular.module('fuApp', ['ngResource', 'angular-files-model']);
        fuApp.controller('fuAppCtr', function fuAppCtr($scope, $resource, $http) {
            $scope.submit = function () {
                if ($scope.fileModel == null) {
                    $scope.fileMessage = "File is empty!";
                }
                else {
                    var file = $scope.fileModel;
                    var dataObj = {
                        image: file[0]
                    };

                    var res = $http.post("http://localhost:10160/UserService/SaveImage", dataObj);
                    res.success(function (data, status, headers, config) {
                        $scope.fileMessage = "Succeed";
                    });
                    res.error(function (data, status, headers, config) {
                        $scope.fileMessage = "Failure : " + JSON.stringify({ data: data });
                    });
                }
            }
        });
    </script>
</body>
</html>

**C# Code**
[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json, UriTemplate = "SaveImage")]
    string PostViaByteArray(byte[] imgBytes);

我在{{fileMessage}}行上获得了以下fileMessage输出;

  

服务器在处理请求时遇到错误。例外   消息是'反序列化类型的对象时出错   System.Byte []。从名称空间“'预期结束元素”root“。发现   来自命名空间''的元素'image'。'。请参阅服务器日志以获取更多详   异常堆栈跟踪是:

     

在   System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator   reader,Boolean verifyObjectName,DataContractResolver   dataContractResolver)at   System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader   reader,Boolean verifyObjectName)at   System.ServiceModel.Dispatcher.SingleBodyParameterDataContractMessageFormatter.ReadObject(消息   消息)   System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(消息   消息,对象[]参数)at   System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息   消息,对象[]参数)at   System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息   消息,对象[]参数)at   System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&安培;   rpc)at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean   isOperationContextSet)

0 个答案:

没有答案