System.NullReferenceException:'对象引用未设置为对象的实例。 postsFile []为空

时间:2019-08-12 07:09:48

标签: sql asp.net-mvc

基本上我正在使用上载文件来处理工作表单,上载了单个文件,没有文件上载的情况下提交了表单。但是,当我添加多个文件上载时,即使它显示错误,我也会添加一些其他条件。你能告诉我我的代码有什么问题吗?提交后,它将进入我的ImageUpload Controller,

//我的表单

        @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());
    }

2 个答案:

答案 0 :(得分:0)

您正在创建一个数组。数组可以为NOT NULL,但仍为空。

代替

project:*,latest:true

尝试:

if (postedFile != null)

答案 1 :(得分:0)

以这种方式检查空值

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))
    {
        ...
    }
}

编辑

尝试改用IFormFile

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

Request.File

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