简体   繁体   中英

System.NullReferenceException: 'Object reference not set to an instance of an object.' postedFile[] was null

Basically i'm doing a working form with an upload file, doing single file upload, submit form without file upload is okay. But when i added an multiple file upload , it displays an error even though, i added some if else condition. can you tell me what's wrong in my code. After submit it will go to my ImageUpload Controller,

//My Form

        @using (Html.BeginForm("ImageUpload", "Ticket", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <div class="container">

                <form>
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    <div class="row">
                        <div class="form-group col-md-3">
                            <label>Date Occurred</label>
                            <input type="hidden" name="DateCreated" id="DateCreated" class="form-control" />
                            <input type="date" name="DateOccured" id="DateOccured" class="form-control" required />
                        </div>
                        <div class="form-group col-md-3">
                            <label>Time Occurrence</label>
                            <input type="hidden" name="TimeCreated" id="TimeCreated" class="form-control" />
                            <input type="time" name="TimeOccured" id="TimeOccured" class="form-control" required />
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-md-12">
                            <label>Subject</label>
                            <input type="text" name="TicketSubject" class="form-control" id="TicketSubject" maxlength="50" />
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-md-4">
                            <label>Error Type</label>
                            <select name="ErrorType" id="ErrorDropdown" class="form-control ErrorType" onchange="DropDownOthers(this.value);">
                                <option value="None">None</option>
                                <option value="Others">Others</option>
                            </select>
                        </div>
                        <div class="form-group col-md-4">
                            <label id="error_label" style="display:none;">Others</label>
                            <input type="text" name="error_type" class="form-control ErrorType" id="ErrorType" style="display:none" />
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label asp-for="Details" class="control-label"></label>
                                <textarea class="form-control" rows="5" id="Details" name="Details"></textarea>
                                <span asp-validation-for="Details" class="text-danger"></span>
                            </div>

                        </div>
                    </div>

                    <input type="file" name="postedFile" multiple class="form-control" />

                    <div class="form-group">
                        <input type="text" name="TicketStatus" class="form-control ErrorType" id="TicketStatus" value="Open" style="display:none" />
                        <input type="submit" id="addTicket" value="Create" class="btn btn-md btn-outline-secondary" style="margin:auto;display:block;" />
                    </div>
                </form>
            </div>


 [HttpPost]
public ActionResult ImageUpload(HttpPostedFileBase[] postedFile ,string DateOccured, string TimeOccured,
        string TicketSubject, string ErrorType , string Details, string TicketStatus)
    {

        string connectionString = ConnectionString.CName;
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            if (postedFile != null)
            {
                for (int i = 0; i < postedFile.Length; i++)
                {
                    byte[] bytes;
                    using (BinaryReader br = new BinaryReader(postedFile[i].InputStream))
                    {
                        bytes = br.ReadBytes(postedFile[i].ContentLength);
                    }
                    SqlCommand cmd = new SqlCommand("spInsertTicket", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    {
                        cmd.Parameters.AddWithValue("@Details", Details);
                        cmd.Parameters.AddWithValue("@TicketSubject", TicketSubject);
                        cmd.Parameters.AddWithValue("@ErrorType", ErrorType);
                        cmd.Parameters.AddWithValue("@DateOccured", DateOccured);
                        cmd.Parameters.AddWithValue("@TimeOccured", TimeOccured);
                        cmd.Parameters.AddWithValue("@TicketStatus", TicketStatus);

                        cmd.Parameters.AddWithValue("@Name", Path.GetFileName(postedFile[i].FileName));
                        cmd.Parameters.AddWithValue("@ContentType", postedFile[i].ContentType);
                        cmd.Parameters.AddWithValue("@Data", bytes);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            else
            {
                SqlCommand cmd = new SqlCommand("spInsertTicket", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Details", Details);
                cmd.Parameters.AddWithValue("@TicketSubject", TicketSubject);
                cmd.Parameters.AddWithValue("@ErrorType", ErrorType);
                cmd.Parameters.AddWithValue("@DateOccured", DateOccured);
                cmd.Parameters.AddWithValue("@TimeOccured", TimeOccured);
                cmd.Parameters.AddWithValue("@TicketStatus", TicketStatus);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }

        }

        return View(GetFiles());
    }

You are creating an array. An array can be NOT NULL but still be empty..

Instead of

        if (postedFile != null)

try:

if (postedFile.length > 0)

Check for the null this way

for (int i = 0; i < postedFile.Length; i++)
{
    //adding this line
    if(postedFile[i] == null || string.IsNullOrEmpty(postedFile[i].FileName))
        continue;
    byte[] bytes;
    using (BinaryReader br = new BinaryReader(postedFile[i].InputStream))
    {
        ...
    }
}

EDIT

Try using IFormFile instead

public ActionResult ImageUpload(List<IFormFile> files)
{
    if(files != null && files.Count > 0)
    {
    }
}

Or Request.File

for (int i = 0; i < Request.Files.Count; i++)
{
   var fileDoc = Request.Files[i];  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM