0

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());
    }
codeSeven
  • 469
  • 3
  • 15
  • In which line you are getting the error? – Code_Pirate. Aug 12 '19 at 07:51
  • Hi, Please have a look at my answer. https://stackoverflow.com/questions/56364222/request-files-is-empty-when-files-come-through-as-an-array/56396099#56396099 – Javid Gahramanov Aug 12 '19 at 10:32
  • @Code_Pirate., if the IF condition, it wont work even i use IF/ELSE if the file is NULL. – codeSeven Aug 12 '19 at 12:00
  • Note: `SqlCommand` (like `SqlConnection` and `BinaryReader`), implements `IDisposable`, and therefore should be wrapped in a `using` construct to ensure proper deterministic disposal of underlying unmanaged resources. – Jesse C. Slicer Aug 12 '19 at 13:29
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Peter Duniho Aug 13 '19 at 04:02

2 Answers2

0

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)

Rule
  • 78
  • 7
  • Sorry, you must check if it's empty AND if its zero. `if (postedFile != null && postedFile.length > 0)` And to be completely sure change your FOR statement to a FOREACH statement. ` for (int i = 0; i < postedFile.Length; i++) ` becomes ` foreach (var file in postedFile)` and replace `postedFile[i] ` with `file` – Rule Aug 14 '19 at 14:16
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];  
}
Bosco
  • 1,388
  • 2
  • 10
  • 18
  • when i added this come. There's no displaying an error., submit form with upload is still okay, but wiithout file upload is still not working. but good thing there;s no error – codeSeven Aug 13 '19 at 03:37
  • Can you show the view where you are uploading the files? make sure it is set to multiple – Bosco Aug 13 '19 at 03:39