简体   繁体   English

从Angular UI将Excel数据发送到Web API服务

[英]Send an excel data to web api service from Angular UI

In my UI project , I have a file uploader control which looks like : 在我的UI项目中,我有一个文件上载器控件,如下所示:

            <div class="col-lg-2 col-sm-2 col-md-2">
                <div style="font-size:14px; ">
                    <label>Upload File</label>
                    <div class="ui-select">
                        <!--<div file-select="file"></div>-->
                        <input type="file" name="files" ng-files="getTheFiles($files)" />
                    </div>

                </div>

            </div>

And the directive ngFiles looks like : 指令ngFiles看起来像:

CaseModule.directive('ngFiles', ['$parse', function ($parse) { CaseModule.directive('ngFiles',['$ parse',function($ parse){

    function fn_link(scope, element, attrs) {
        var onChange = $parse(attrs.ngFiles);
        element.on('change', function (event) {
            onChange(scope, { $files: event.target.files });
        });
    };

    return {
        link: fn_link
    }
}]);

So, in the controller code I read the uploaded files as 因此,在控制器代码中,我读取了上传的文件

   //UPLOAD FILE CODE
    var formdata;
    $scope.getTheFiles = function ($files) {
        formdata = new FormData();
        angular.forEach($files, function (value, key) {
            formdata.append(key, value);
        });
    };

    // NOW UPLOAD THE FILES.
    $scope.uploadFiles = function () {
        var request = {
            method: 'POST',
            url: BasePath + 'caseNative/UploadFiles/',
            data: formdata,
            headers: {
                'Content-Type': undefined
            }
        };

Now I dont know how to send this excel data to the server . 现在我不知道如何将这些excel数据发送到服务器。 Could someone help me n this regard ? 在这方面有人可以帮我吗? I want to be able to read the excel data . 我希望能够读取Excel数据。

Thanks in advance. 提前致谢。

With Asp.Net WebApi MVC, create a controller such as FileUploadController . 使用Asp.Net WebApi MVC,创建诸如FileUploadController的控制器。

Prefix a API route to the API you are using. 将API路由前缀添加到您正在使用的API。 HttpContext library that would read any files over the said API. HttpContext库,它将通过所述API读取任何文件。

public class FileUploadController : ApiController
{
  [Route("caseNative/UploadFiles/")]
  [HttpPost]

  public HttpResponseMessage UploadFile()
  {
     HttpResponseMessage response = null;
     try
     {
        if (HttpContext.Current.Request.Files.AllKeys.Any()){
            //If any file exist, then it would be available in the key
            //you have appended while creating a formdata.
            var httpPostedUploadFile = HttpContext.Current.Request.Files["Key"];
        }
     }
     catch (Exception e)
     {
        throw ex;
     }
}

Above code works for single file upload, if the request contains more than one file, you could modify the code to loop around the files. 上面的代码适用于单个文件上传,如果请求包含多个文件,则可以修改代码以使文件循环。

Further more, you could read the Documentation here. 此外,您可以在此处阅读文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM