简体   繁体   中英

Sending big string over JSON post to server with ASP.NET 5 - CORS error

I'm having the problem with sending a file as a byte array json string to the server. When the file is relatively small: up to 2MB or so, then it works fine, but if the file is like 4MB then the request throws error (it doesnt even reach the controller in the backend).

Error:

XMLHttpRequest cannot load http://localhost:61416/api/organisationdocuments/.
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:51080' is therefore not allowed access. 
The response had HTTP status code 404.

In the startup.cs I have

 // Add CORS support.
        services.AddCors(options =>
        {
            options.AddPolicy("AAPolicy", policyBuilder =>
                policyBuilder
                    .AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials()
                );
        });

In the front-end I use angularjs . The method looks like this:

this.addFile = function (file) {
            return $http.post(fileApi, file);
        };

I debuged it on the front and it looks alright, except the string with the byte array is so big it hangs my browser sometimes. So As I mentioned, it works for smaller files, only occurs for the bigger ones. It obviously has to do something with the weight of the file.

I tried to debug back-end, but it doesnt even reach the controller, so there is no way. Any ideas? Thanks a lot!

Edit:

My web.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <httpRuntime maxRequestLength="10000000" />
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="10000000" />
      </requestFiltering>
    </security>
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
    </handlers>
    <httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600" />
  </system.webServer>
</configuration>

add this to your Web.Config , it will allow you to upload the content up to 10MB. (10000000 = 10 MB)

<system.web>
  <httpRuntime maxRequestLength="10000000" />
</system.web>

<system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="10000000" />
      </requestFiltering>
    </security>
</system.webServer>

Hope this help

you must using chunk approach to do that or use stream array

check this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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