Base solution for your next web application
Open Closed

Error when try to upload file with AbpController #1198


User avatar
0
soulmate created

Hello,

I have following code:

public class OfferFileController : AbpController
    {

        private readonly ILogger _logger;
        private readonly IMapper _mapper;
        private readonly IOfferFileService _offerFileService;
        private readonly IOfferService _offerService;
        private readonly UserManager _userManager;

        
        public OfferFileController(IOfferService offerService, UserManager userManager,
            IOfferFileService offerFileService,
            IMapper mapper,
            ILogger logger)
        {
            _offerService = offerService;
            _offerFileService = offerFileService;
            _userManager = userManager;
            _mapper = mapper;
            _logger = logger;
            
        }

        public ActionResult UploadFiles(IEnumerable<HttpPostedFileBase> files, int offerId)
        {
            // The Name of the Upload component is "files"
            if (files != null)
            {
                foreach (var file in files)
                {
                    var offerFile = new OfferFile();
                    var fileName = Path.GetFileName(file.FileName);

                    //Check if file exist
                    offerFile = _offerFileService.GetByNameAndOfferId(fileName, offerId) ?? new OfferFile();

                    offerFile.Name = fileName;
                    offerFile.FileType = file.ContentType;
                    offerFile.OfferId = offerId;
                    using (var binaryReader = new BinaryReader(file.InputStream))
                    {
                        offerFile.File = binaryReader.ReadBytes(file.ContentLength);
                    }

                    //Save
                    _offerFileService.Save(offerFile);

                }
            }

            // Return an empty string to signify success
            return Content("");
        }

When I pass a file I receive

Object reference not set to an instance of an object. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 



[NullReferenceException: Object reference not set to an instance of an object.]
   Abp.Web.Mvc.Controllers.AbpController.ConvertArgumentsToJson(IDictionary`2 arguments) in D:\Halil\GitHub\aspnetboilerplate\src\Abp.Web.Mvc\Web\Mvc\Controllers\AbpController.cs:515
   Abp.Web.Mvc.Controllers.AbpController.HandleAuditingBeforeAction(ActionExecutingContext filterContext) in D:\Halil\GitHub\aspnetboilerplate\src\Abp.Web.Mvc\Web\Mvc\Controllers\AbpController.cs:428
   Abp.Web.Mvc.Controllers.AbpController.OnActionExecuting(ActionExecutingContext filterContext) in D:\Halil\GitHub\aspnetboilerplate\src\Abp.Web.Mvc\Web\Mvc\Controllers\AbpController.cs:299

However, if I replace the AbpController with the default Controller (meaning public class OfferFileController : Controller) it works. Do you have any idea why this happens?


2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    It seems related to audit logging. Can you add [DisableAuditing] attribute to your UploadFiles method to solve your problem. We will consider this in next versions.

    Have a nice day.

  • User Avatar
    0
    soulmate created

    Thanks, this fixed my problem