Open Closed

File upload issue #1226


User avatar
0
RonaldR created
  • ABP Framework version: v4.2.2

  • UI type: MVC

  • DB provider: EF Core

  • Tiered (MVC) or Identity Server Separated (Angular): no

  • Exception message and stack trace:

  • Steps to reproduce the issue:

  • I am trying to upload a file, should be simple enough. i have this controller:

  • public class SourceOfTruthAttributesAppService : ApplicationService
    

    {

      public async Task<IActionResult> PostUploadFile([FromBody]IFormFile file)
      {
          try
          {
              using (var stream = new MemoryStream())
              {
                  await file.CopyToAsync(stream);
              }
    
              return new OkResult();
          }
          catch (Exception ex)
          {
              return new BadRequestObjectResult(ex.Message);
          }
      }
    

    }

    i have this jquery code:
    $("#but_upload").click(function () {
    event.preventDefault();

      alert('begin upload');
    
      if ($('#file').length == 0) {
          alert('No file to upload');
          return;
      }
    
      var fd = new FormData();
      var file = $('#file')[0].files[0];
      fd.append('file', file);
    
      abp.ajax({
          type: 'POST',
          data: fd,
          processData: false,
          contentType: false,
          url: "/api/app/source-of-truth-attributes/upload-file",
      }).then(function (result) {
          alert('your file has been uploaded');
          location.reload();
      });
    

    })

    it works with null in the data, but when i try to pass the file in to the data i get the following error:
    jquery.js?_v=637504727666307175:9600 POST https://localhost:44355/api/app/source-of-truth-attributes/upload-file 415
    send @ jquery.js?_v=637504727666307175:9600
    ajax @ jquery.js?_v=637504727666307175:9206
    (anonymous) @ abp.jquery.js?_v=637504727661778549:112
    Deferred @ jquery.js?_v=637504727666307175:3751
    abp.ajax @ abp.jquery.js?_v=637504727661778549:111
    (anonymous) @ index.js?_v=637549962657651648:58
    dispatch @ jquery.js?_v=637504727666307175:5183
    elemData.handle @ jquery.js?_v=637504727666307175:4991

    what am i doing wrong? thanks


2 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Try FromForm

    PostUploadFile([FromForm]IFormFile file)
    
  • User Avatar
    0
    RonaldR created

    thanks

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.2.0-preview. Updated on March 13, 2025, 04:08