【问题标题】:Unable send excel file data to web api无法将 excel 文件数据发送到 web api
【发布时间】:2016-10-02 01:15:09
【问题描述】:

在我的 Angular 应用程序中,我正在尝试从 Angular JS 将 excel 文件上传到 web api,但我无法这样做。 服务端 web api 中的方法没有被命中。

角码看起来像:

    $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 () {
                });
        }

并且应该调用 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);
    }

此 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))
                {

...响应是 Http Error 415 “Unsupported Media Type”。它不调用服务。

【问题讨论】:

    标签: angularjs excel file-upload asp.net-web-api content-type


    【解决方案1】:

    headers 更改为:

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

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 1970-01-01
      • 2017-06-09
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多