0

Hi everyone I am creating an application using asp.net mvc with a codefirst database that allows the user to store an image along with some text in the database but I want the user to be able submit the text by itself with no images. The issue is that currently if the user submits the text without the image it crashs on this line if (file.ContentLength > 0) and gives a null reference exception. Thank you for anyhelp.

 foreach (HttpPostedFileBase file in model.Files)
            {
                if (file.ContentLength > 0)
                {
                    string fileName = file.FileName;
                    string Extension = Path.GetExtension(fileName);
                    string path = Path.Combine(Server.MapPath("~/Images/"), fileName);
                    file.SaveAs(path);
                    MyModel image = new MyModel()
                    {
                        Path = fileName,
                        FileName = fileName
                    };
                    post.Images.Add(image);
                }
            }

view

  @Html.TextBoxFor(model => model.Files, new { type = "file", multiple = "multiple" })
  • It crashes on the `foreach` line because the `Files` property of model is `null` - `if (model.Files != null) { foreach(....` –  Feb 02 '17 at 12:05
  • try file.ContentLength != null && file.ContentLength > 0. no need for model.file checks , if there are no files in the object you wont go into the the foreach loop. – KevDevMan Feb 02 '17 at 12:07
  • @KevDevMan, you do need to check for `null` (you cannot iterate over `null`) –  Feb 02 '17 at 12:13
  • @KevDevMan it says the expression is always true as int can never be equal to null, so i tried file.ContentLength !=0 but same issue – ARandomUserNamedChuck Feb 02 '17 at 12:16
  • @StephenMuecke it ignores the if (model.Files != null) it says file is equal to {System.Web.HttpPostedFileBase[1]} when I debug it even though there is no images submited – ARandomUserNamedChuck Feb 02 '17 at 12:28
  • @StephenMuecke so i wrote model.Files = null; above the if just to check if it was because it says file|{System.Web.HttpPostedFileBase[1]} and it turns out it is for some reason my default even if nothing is submited is equal to that? – ARandomUserNamedChuck Feb 02 '17 at 12:50
  • Yes @StephenMuecke that is true. But as his error is after the first foreach and not before there is no point in checking for model.file != null. He has a file, so thats not the issue. If the model.File was null it would crash on the foreach and not after. – KevDevMan Feb 02 '17 at 14:07
  • If it really is crashing on `if (file.ContentLength > 0)` then just change it to `if (file != null && file.ContentLength > 0)` but that fact its null suggests there is something wrong with other code in your app. –  Feb 02 '17 at 22:30
  • But I suspect the real line of code where that is failing is `post.Images.Add(image);` because you have not initialized the `Images` property of your model and its `null` (you cannot call `.Add()` on `null`) –  Feb 03 '17 at 22:39

1 Answers1

0

Hello @ARandomUserNamedChuck ,

I don't have solid solution but I think a way to fix this should be this theoretically: You should use a for loop and convert both text and the image fields into a string by using .length . After this you call the field where the upload goes and if its empty but text field has something on then you still continue the process. Hopefully this approach helps.

PMA
  • 91
  • 2
  • 13