简体   繁体   English

无法将Excel文件数据发送到Web API

[英]Unable send excel file data to web api

In my angular app , I am trying to upload an excel file to web api from Angular JS but I am unable to do so . 在我的angular应用程序中,我试图将excel文件从Angular JS上传到Web api,但无法这样做。 The method in the server web api is not being hit. 服务器Web API中的方法未命中。

The angular code looks like : 角度代码如下所示:

    $scope.file = null;

    //UPLOAD FILE CODE
    var formdata;
    $scope.getTheFiles = function ($files) {
        console.log($files[0].type);
        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 + 'uploadNative/UploadFiles/',
            data: formdata,
            headers: {
                'Content-Type': undefined
            }
        };

        // SEND THE FILES.
        console.log(formdata);

        if (formdata != null || formdata != undefined) {
            $http(request)
                .success(function (response) {
                    if (response != "Failed!") {
                        console.log("Succeeds");
                    }
                    else {
                        console.log("Failed");
                    }
                })
                .error(function () {
                });
        }

And the UI MVC controller which should invoke the web api controller causes exception. 并且应该调用Web api控制器的UI MVC控制器导致异常。

    public async Task<JsonResult> UploadFiles()
    {
        System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

        string url = BaseURL + "api/Upload/groupmembershipupload/";
        string res = await Models.Resource.PostFileAsync(url, Token, hfc, ClientID);
        var result = (new JavaScriptSerializer()).DeserializeObject(res);
        return Json(result);
    }

This PostFileAsync fires the exception. 此PostFileAsync引发异常。

            using (client = new HttpClient())
            {

                client.BaseAddress = new Uri(url);
                client.DefaultRequestHeaders.Accept.Clear();
                //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + oauthToken);
                client.DefaultRequestHeaders.Add("ClientID", ClientID);
                var content = new MultipartFormDataContent();
                System.Web.HttpPostedFile hpf = data[0];


                byte[] fileData = null;
                using (var sds = new BinaryReader(hpf.InputStream))
                {
                    fileData = sds.ReadBytes(hpf.ContentLength);
                }
                content.Add(new ByteArrayContent(fileData, 0, fileData.Count()));
                //using (response = await client.PostAsJsonAsync(url + "?=" + DateTime.Now.Ticks, data))
                using (response = await client.PostAsync(url + "?=" + DateTime.Now.Ticks, content))
                {

...the response is Http Error 415 "Unsupported Media Type". ...响应是Http错误415 “不支持的媒体类型”。 It does not call the service. 它不调用服务。

Change the headers to: headers更改为:

headers: {
            'Content-Type': 'multipart/form-data'
        }

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

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