为什么我得到空值当一个文件和多个文件提交给控制器?

时间:2017-11-13 10:41:08

标签: c# asp.net-mvc

我有一个带有三个参数的控制器:两个是HttpPostedFileBase文件和IEnumerable<HttpPostedFileBase>文件,还有一个包含两个输入文件的表单,单个和多个。但是当我将表单提交给控制器时,我遇到了问题。我发现object reference not set to an instance of an object错误。但是当我有一个HttpPostedFileBase文件时,我没有任何问题。

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ProductId,Name,ImageName,LargeImageName,EnglishName,Price,ShortDescription,Keyword,PageVisit,Status,CategoryId")] Good good, HttpPostedFileBase file, IEnumerable<HttpPostedFileBase> files)
    {
        if (ModelState.IsValid)
        {
            if (file.ContentLength > 0)
            {
                var versions = new Dictionary<string, string>();

                var path = Server.MapPath("/UploadFiles/Images/GoodGallery/");
                versions.Add("_small", "maxwidth=270&maxheight=180&format=jpg");
                versions.Add("_large", "maxwidth=600&maxheight=400&format=jpg");
                foreach (var suffix in versions.Keys)
                {
                    file.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
                    ImageBuilder.Current.Build(new ImageJob(file.InputStream, path + file.FileName + suffix, new Instructions(versions[suffix]), false, true));
                }


                //good. = DateTime.Now.ToString();
                var dotPosition = file.FileName.IndexOf('.');
                var largeFileName = file.FileName.Insert(dotPosition, ".jpg_large");
                var fileName = file.FileName.Insert(dotPosition, ".jpg_small");
                good.LargeImageName = largeFileName;
                good.ImageName = fileName;

            }

            foreach (var file2 in files)
            {
                var versions = new Dictionary<string, string>();

                var path = Server.MapPath("/UploadFiles/Images/GoodGallery/");
                versions.Add("_small", "maxwidth=270&maxheight=180&format=jpg");
                versions.Add("_large", "maxwidth=600&maxheight=400&format=jpg");
                foreach (var suffix in versions.Keys)
                {
                    file2.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
                    ImageBuilder.Current.Build(new ImageJob(file2.InputStream, path + file2.FileName + suffix, new Instructions(versions[suffix]), false, true));
                }



                var dotPosition = file2.FileName.IndexOf('.');
                var largeFileName = file2.FileName.Insert(dotPosition, ".jpg_large");
                var smallfileName = file2.FileName.Insert(dotPosition, ".jpg_small");
                good.Images.Add(new ImageList { LargeImageName = largeFileName, SmallImageName = smallfileName });

            }

            ViewBag.Message = "فایل با موفقیت آپلود شد.";


            //return RedirectToAction("Index");
        }

        db.Goods.Add(good);
        db.SaveChanges();

        ViewBag.CategoryId = new SelectList(db.Categories, "Id", "CategoryName", good.CategoryId);
        return View(good);

    }




    @using (Html.BeginForm("Create", "Good", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()


<div class="row">
    <div class="col-lg-12">
        <div class="card card-outline-info">
            <div class="card-header">
                <h4 class="m-b-0 text-white">فرم محصول</h4>
            </div>
            <div class="card-body">
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="form-body">
                    <h3 class="box-title">ایجاد محصول</h3>
                    <hr class="m-t-0 m-b-40">
                    <div class="row">

                <div class="form-horizontal">
                    <h3 class="box-title">آپلود تصویر</h3>
                    <hr class="m-t-0 m-b-40">

                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">

                                <form action="#" class="dropzone">
                                    <div class="fallback">
                                        <input name="file" type="file" />
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <h3 class="box-title">آپلود تصاویر اسلایدر</h3>
                    <hr class="m-t-0 m-b-40">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">

                                <form action="#" class="dropzone">
                                    <div class="fallback">
                                        <input name="files" type="file" multiple/>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>

                    <hr>
                    <div class="form-actions">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="row">
                                    <div class="col-md-offset-3 col-md-9">
                                        <button type="submit" class="btn btn-success btn-lg">ثبت</button>
                                        <button type="button" class="btn btn-inverse btn-lg">بازگشت</button>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6"> </div>
                        </div>


                    </div>
                </div>
            </div>
            <!--/row-->

}

0 个答案:

没有答案