2

This error is making me crazy.

I am trying to upload an image via ajax in my MVC project, it works perfectly locally in IIS Express and also in normal IIS in my local machine, but not in my server.

My local IIS and the IIS in my VPS Server are the same version (8.5.9600.16384), and I have another 10 projects running in this server without problems.

Here is the stacktrace:

[NullReferenceException: Object reference not set to an instance of an object.]
   DoVase.MVC.Controllers.OrdersController.UploadFiles() +1024
   lambda_method(Closure , ControllerBase , Object[] ) +86
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +259
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +34
   System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +32
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +41
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +79
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +386
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +386
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +30
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +900
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +1299
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +27
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +22
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +22
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1460
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +2650
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +979

And here my javascript:

$(document).on('change', '.btn-file :file', function () {
            var input = $(this);

            var files = input.get(0).files;
            var data = new FormData();
            for (i = 0; i < files.length; i++) {
                data.append("file" + i, files[i]);
            }

            input.parent().attr('disabled', 'disabled');
            $(".file-button-text").text($("#hdnTextLoading").val());

            $.ajax({
                type: "POST",
                url: "es/Orders/UploadFiles",
                contentType: false,
                processData: false,
                data: data,
                success: function (result) {
                    if (result) {
                        //Put the urlImg in a hidden field.
                        $("#hdnUrlImage").val(result);
                        //Is a success, go to the result page.
                        $("#formContinueWithOrder").submit();
                    }
                }
            });
        });

The part of the view.

<span class="file-input btn btn-style btn-file btn-large">
    <span class="file-button-text">@Resources.Text.upload_picture</span>
    <input type="file">
</span>

And my action in the controller.

        [HttpPost]
        [Localization]
        public ActionResult UploadFiles()
        {
            try
            {
                var name = string.Empty;

                if (Request == null || Request.Files == null)
                {
                    return Content("ERROR NO FILES SENDED!");
                }

                // Save the images.
                foreach (string file in Request.Files)
                {
                    var imageStream = Request.Files[file];
                    var image = Image.FromStream(imageStream.InputStream);
                    name = DateTime.Now.Ticks + imageStream.FileName;

                    var path = Server.MapPath(@"\UploadImages\");

                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    //imageStream.SaveAs(path + name);
                    image.Save(path + name);
                }

                // Return the id of the order.
                if (Request.IsAjaxRequest())
                {
                    return Content(name);
                }
                else
                {
                    return MakeNotDropzone(name);
                }
            }
            catch (Exception ex)
            {
                var text = ex.Message + "<br/>";
                text += ex.InnerException != null ? ex.InnerException.Message : "NO INNER EX";
                text += ex.InnerException.StackTrace != null ? ex.InnerException.StackTrace : "NO INNER STACKTRACE";
                return Content(text);
            }
        }

I have the same problem if I use imageStream.SaveAs or image.Save I now the problem is from this line of code, because if I remove the line, and publish again in the server, it doesn't fail.

Thank you in advance.

SirMartin
  • 61
  • 5
  • Welcome to StackOverflow! Since it works on your other machines I hesitate to mark this as a duplicate, but just to be sure, take a look at http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it?s=1|8.8313 – René Vogt Mar 13 '16 at 20:48
  • Thanks Rene. I have checked also to make some if to check if its NULL or not the image variable, but I always have the same problem, so because this I am so confuse with the problem. I always try to upload the same file in all the machines, and same browser and computer for test. – SirMartin Mar 14 '16 at 10:34

0 Answers0