Answer the question
In order to leave comments, you need to log in
How to properly implement file upload in Asp.net MVC?
I implement the functionality of the pressure of the text section on the site.
It works, but there is an attachment of pictures to the section. And if there are pictures, that's fine too. and without pictures does not want.
throws: Exception Details: System.NullReferenceException: Object reference does not point to an instance of an object.
public ActionResult Add(
List<HttpPostedFileBase> FolderImages,
.......
)
{
....
if (FolderImages.Count<HttpPostedFileBase>()>0)
{
foreach(HttpPostedFileBase Image in FolderImages)
{
string Path = "/Uploads/"+ DateTime.Now.GetHashCode() + Image.FileName;
.....
.....
}
}
Answer the question
In order to leave comments, you need to log in
Solved the problem in the following way.
foreach (string file in Request.Files)
{
HttpPostedFileBase Image = Request.Files[file] as HttpPostedFileBase;
........
}
List<HttpPostedFileBase> FolderImages
from method arguments.
If NullReferenceException in this line
"string Path = "/Uploads/"+ DateTime.Now.GetHashCode() + Image.FileName;"
then apparently Image == null
A similar problem is described here MVC. HttpPostedFileBase is always null
In short - instead of
<input type="file" name="files[0]" id="files[0]" />
<input type="file" name="files[1]" id="files[1]" />
<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question