Ajax将大型base64字符串发布到Web Api失败

时间:2016-09-09 14:24:54

标签: ajax asp.net-web-api

我将一个ajax帖子发送到一个控制器,该控制器包含一个base64编码的字符串以及一些其他数据对象。 现在,我有一个简单的控制器,可以将静态图像写入文件夹。

以下代码

  [System.Web.Http.Route("api/Assessment/saveLvlTAssessment1")]
        [System.Web.Http.HttpPost]
        [EnableCors("*", "*", "*")]
        public HttpResponseMessage SaveAssessment1([FromBody] MobileLvlTAssessmentModel model)
        {
            var response = new HttpResponseMessage();
            String path = System.Web.Hosting.HostingEnvironment.MapPath(WebConfigurationManager.AppSettings["ImagePath"]); //Path
            string image = model.Image;
            try
            {
                string fileName = path + "/Test#@@#.jpg";

                byte[] imageBytes = Convert.FromBase64String(image);
                using (FileStream
                    fileStream = new FileStream(fileName, FileMode.Create))
                {
                    // Write the data to the file, byte by byte.
                    for (int i = 0; i < imageBytes.Length; i++)
                    {
                        fileStream.WriteByte(imageBytes[i]);
                    }

                    // Set the stream position to the beginning of the file.
                    fileStream.Seek(0, SeekOrigin.Begin);
                }
            }


            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.Accepted, ex.ToString());
                throw ex;
            }

            if (response.Content == null)
            {
                response = Request.CreateResponse(HttpStatusCode.Accepted, "Saved");
            }
            return response;
        }

我的ajax电话是这样的

  SaveLvlTAssessment: function (data) {
            var deferred = q.defer();
            window.app.ajaxSetUp()
                .then(function (authdata) {
                    $.ajax({
                        url: window.routeDictionary.incident.SaveLvlTAssessment,
                        type: "POST",
                        contentType: "application/json",
                        dataType: "json",
                        data: JSON.stringify(data),
                        beforeSend: function (request) {
                            request.setRequestHeader("Authorization", "Basic " + authdata);
                        }
                    }).then(function (data, textStatus, jqXHR) {
                        delete jqXHR.then; // treat xhr as a non-promise
                        deferred.resolve(data);
                    }, function (jqXHR, textStatus, errorThrown) {
                        helperPlugins.removeWait();
                        deferred.reject(new Error(jqXHR));
                    });
                });
            return deferred.promise;
        }

我已将此添加到我的web.config

<security>
      <requestFiltering>
    <requestLimits maxAllowedContentLength="10485760" />
      </requestFiltering>
            <authentication>
                <anonymousAuthentication userName="dot\achatterjee" password="[enc:AesProvider:ojOlpPI0WUJNmGMlrVoBzBqwj4XpXW8cX2vVI9KY5QZLLOfxJ/inyx8ZR6fVSo5S:enc]" />
                <windowsAuthentication enabled="false" />
            </authentication>
    </security>

我的global.asax.cs有这个

GlobalConfiguration.Configuration.Formatters.FormUrlEncodedFormatter.ReadBufferSize = 256 * 1024; // 256 KB

我的帖子请求在远程服务器中始终失败。但是,如果我不发送图像数据,它会通过。

将头发分开几个小时,会欣赏任何想法或提示。

0 个答案:

没有答案