Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

230
Visualizações
Error when trying to send a 100mb byte array from js to c# using ajax

So i have been going at this for a few days and Im running into an issue. Im trying to make an ajax call that sends a byte[] to the controller in c#.

var data = { FtpUpload: self.Upload(), Attachment: self.Attachment() };
Base.ServiceCall("/Home/Upload", "post", data, true, function  (response) {...}

self.Attachment() contains a property for the 'byte[]'. Im using this to read the byte array from the file which works as planned.

self.OnImageUpload = function (vm, evt) {
  var file = evt.target.files[0];
  var reader = new FileReader();

  reader.onload = function () {
    var attachment = {
      ...
      Attachment: reader.result.split(',')[1]
    };
    self.Attachment(attachment);
  }

  reader.readAsDataURL(file);
};

When i do this with a 17mb file it works ok but when i do this with a 109mb file it throws a 500 error that says this.

[OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
   System.Web.Util.HttpEncoder.UrlDecode(Byte[] bytes, Int32 offset, Int32 count, Encoding encoding) +76
   System.Web.HttpValueCollection.FillFromEncodedBytes(Byte[] bytes, Encoding encoding) +164
   System.Web.HttpRequest.FillInFormCollection() +77

[HttpException (0x80004005): The URL-encoded form data is not valid.]
   System.Web.HttpRequest.FillInFormCollection() +130
   System.Web.HttpRequest.EnsureForm() +69
   System.Web.HttpRequest.get_Form() +13
   System.Web.HttpRequestWrapper.get_Form() +14

I have added the proper settings to my web.config to allow for larger files like so.

<system.web>  
  <httpRuntime targetFramework="4.6" maxRequestLength="2147483647" executionTimeout="1600" requestLengthDiskThreshold="2147483647" />
</system.web>

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

Im not sure what else to do.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Based on the stack trace your call is failing inside HttpEncoder.UrlDecode which is only possible if you are sending your image as application/x-www-form-urlencoded which may or may not be the best option when you are dealing with heavy binary data.

Regardless of the content type you choose to use you would have to re-implement your post action using HttpPostedFileBase parameter which provides you with the InputStream.Read method instead of expecting the byte[] from the request form, see below.

[HttpPost]
public JsonResult Upload(HttpPostedFileBase uploadedFile)
...
byte[] fileByteArray = new byte[uploadedFile.ContentLength];
uploadedFile.InputStream.Read(fileByteArray, 0, uploadedFile.ContentLength);
...

You could see in the example that the action parameter has to be HttpPostedFileBase in order to stream file instead of sending/receiving as a whole array of binary data.

Have a look at the following project below https://www.codeproject.com/Articles/1021004/Upload-File-Using-Ajax-And-HTML-in-MVC

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda