HttpPostfile 在尝试上传图像时返回空值

本文关键字:图像 返回 空值 HttpPostfile | 更新日期: 2023-09-27 18:32:53

我正在尝试在 mvc 中上传图像,但它一直返回 null。

控制器:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult PostRollOutForm([Bind(Include = "TankSerNo,NozzleSerNo,NozzleLocationDescript,NozzleIdentifier,NozzleFunction,NozzleType,NozzleDiameterorHeight,NozzleWidth,NozzleLocation,NozzleHeight,NozzleWeldSize,NozzleThickness,NozzleComments,RepadShape,RepadHeight,RepadWidth,RepadWeldSize,RepadThickness,TelTale,WeldSpacetoCornerWeld,WeldSpacetoVerticalWeld,WeldSpacetoAdjacentNozzle,WeldSpacetoHorizontalWeld,WeldDetail,CoverThickness,LengthToFlange,LenghtToCtrofValve,CenterLineorLowerHeight,DrawCircleorNot,CornerRadius,Dimension,Label,NozzleThicknessT,NozzleThicknessR,NozzleThicknessB,NozzleThicknessL,Photo")] RollOutViewModel rollout, HttpPostedFileBase uploadedfile, string FailURL)
    {
        if (ModelState.IsValid)
        {
            //upload image attempt 1 
            if(uploadedfile != null) { 
                RollOutFileUploadService service = new RollOutFileUploadService();
                service.SaveFileDetails(uploadedfile, rollout.NozzleSerNo);
            }
            //attempt 2                
            else if (rollout.Photo != null)
            {
                RollOutFileUploadService service = new RollOutFileUploadService();
                service.SaveFileDetails(uploadedfile, rollout.NozzleSerNo);
            }
            //form fields
            ShellRollOut result = new ShellRollOut();
            result.TankSerNo = rollout.TankSerNo;
            result.NozzleSerNo = rollout.NozzleSerNo;
            result.NozzleLocationDescript = rollout.NozzleLocationDescript;
            result.NozzleIdentifier = rollout.NozzleIdentifier;
            result.NozzleFunction = rollout.NozzleFunction;
            result.NozzleType = rollout.NozzleType;
            result.NozzleDiameterorHeight = rollout.NozzleDiameterorHeight;
            result.NozzleWidth = rollout.NozzleWidth;
            result.NozzleLocation = rollout.NozzleLocation;
            result.NozzleHeight = rollout.NozzleHeight;
            result.NozzleWeldSize = rollout.NozzleWeldSize;
            result.NozzleThickness = rollout.NozzleThickness;
            result.NozzleComments = rollout.NozzleComments;
            result.RepadShape = rollout.RepadShape;
            result.RepadHeight = rollout.RepadHeight;
            result.RepadWidth = rollout.RepadWidth;
            result.RepadWeldSize = rollout.RepadWeldSize;
            result.RepadThickness = rollout.RepadThickness;
            result.TelTale = rollout.TelTale;
            result.WeldSpacetoCornerWeld = rollout.WeldSpacetoCornerWeld;
            result.WeldSpacetoVerticalWeld = rollout.WeldSpacetoVerticalWeld;
            result.WeldSpacetoAdjacentNozzle = rollout.WeldSpacetoAdjacentNozzle;
            result.WeldSpacetoHorizontalWeld = rollout.WeldSpacetoHorizontalWeld;
            result.CoverThickness = rollout.WeldSpacetoHorizontalWeld;
            result.WeldDetail = rollout.WeldDetail;
            result.LengthToFlange = rollout.LengthToFlange;
            result.LenghtToCtrofValve = rollout.LenghtToCtrofValve;
            result.CenterLineorLowerHeight = rollout.CenterLineorLowerHeight;
            result.DrawCircleorNot = rollout.DrawCircleorNot;
            result.CornerRadius = rollout.CornerRadius;
            result.Dimension = rollout.Dimension;
            result.Label = rollout.Label;
            result.NozzleThicknessT = rollout.NozzleThicknessT;
            result.NozzleThicknessR = rollout.NozzleThicknessR;
            result.NozzleThicknessB = rollout.NozzleThicknessB;
            result.NozzleThicknessL = rollout.NozzleThicknessL;
            result.Tank = rollout.Tank;
            db.ShellRollOuts.Add(result);
            db.SaveChanges();
            string url = Url.Action("ShellRollOut", new { TankSerNo = rollout.TankSerNo });
            return Json(new { success = true, url = url }, JsonRequestBehavior.AllowGet);
        }
        return PartialView(FailURL, rollout);
    }

查看模型:

   public class RollOutViewModel
{
    public Nullable<int> TankSerNo { get; set; }
    public int NozzleSerNo { get; set; }
    public string NozzleLocationDescript { get; set; }
    public string NozzleIdentifier { get; set; }
    public string NozzleFunction { get; set; }
    public string NozzleType { get; set; }
    public Nullable<float> NozzleDiameterorHeight { get; set; }
    public Nullable<float> NozzleWidth { get; set; }
    public Nullable<float> NozzleLocation { get; set; }
    public Nullable<float> NozzleHeight { get; set; }
    public Nullable<float> NozzleWeldSize { get; set; }
    public Nullable<float> NozzleThickness { get; set; }
    public string NozzleComments { get; set; }
    public string RepadShape { get; set; }
    public Nullable<float> RepadHeight { get; set; }
    public Nullable<float> RepadWidth { get; set; }
    public Nullable<float> RepadWeldSize { get; set; }
    public Nullable<float> RepadThickness { get; set; }
    public string TelTale { get; set; }
    public Nullable<float> WeldSpacetoCornerWeld { get; set; }
    public Nullable<float> WeldSpacetoVerticalWeld { get; set; }
    public Nullable<float> WeldSpacetoAdjacentNozzle { get; set; }
    public Nullable<float> WeldSpacetoHorizontalWeld { get; set; }
    public string WeldDetail { get; set; }
    public Nullable<float> CoverThickness { get; set; }
    public string LengthToFlange { get; set; }
    public string LenghtToCtrofValve { get; set; }
    public string CenterLineorLowerHeight { get; set; }
    public string DrawCircleorNot { get; set; }
    public Nullable<float> CornerRadius { get; set; }
    public string Dimension { get; set; }
    public string Label { get; set; }
    public Nullable<float> NozzleThicknessT { get; set; }
    public Nullable<float> NozzleThicknessR { get; set; }
    public Nullable<float> NozzleThicknessB { get; set; }
    public Nullable<float> NozzleThicknessL { get; set; }
    public HttpPostedFileBase Photo { get; set; }
    public virtual Tank Tank { get; set; }
}

视图:

@using (Html.BeginForm("PostRollOutForm", "Tanks", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="modal-body">
    <div class="form-horizontal">
        @Html.HiddenFor(x => x.NozzleIdentifier, new { @Value = "" })
        @Html.HiddenFor(x => x.TankSerNo)
        @Html.HiddenFor(x => x.Tank)
        @Html.HiddenFor(x => x.NozzleSerNo)
        <h4>Nozzle Data</h4>
        <div class="form-group">
            @Html.LabelFor(model => model.NozzleFunction, "Nozzle Function", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.NozzleFunction, new List<SelectListItem>
                        {
                            new SelectListItem { Text =  "Spiral Stairway", Value = "Spiral Stairway", Selected = true},
                            new SelectListItem { Text =  "Cat Walk", Value = "Cat Walk"},
                            new SelectListItem { Text =  "Vertical Ladder", Value = "Vertical Ladder"},
                            new SelectListItem { Text =  "Platform", Value = "Platform"},
                            new SelectListItem { Text =  "Radial Stairway", Value = "Radial Stairway"},
                        },
             new { @class = "form-control " })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.NozzleDiameterorHeight, "Hand Rail Height", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.EditorFor(model => model.NozzleDiameterorHeight, new { htmlAttributes = new { @id = "HandRailHeight", @class = "form-control " } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.NozzleHeight, "Height Step or Rise", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.EditorFor(model => model.NozzleHeight, new { htmlAttributes = new { @id = "HeightStep", @class = "form-control " } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.NozzleWidth, "Width of Run", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.EditorFor(model => model.NozzleWidth, new { htmlAttributes = new { @id = "WidthRun", @class = "form-control " } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.RepadHeight, "Last Step", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.EditorFor(model => model.RepadHeight, new { htmlAttributes = new { @id = "RepadHeight", @class = "form-control " } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.RepadWidth, "Cage Height", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.EditorFor(model => model.RepadWidth, new { htmlAttributes = new { @id = "RepadWidth", @class = "form-control " } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.RepadShape, "Shape of Access", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.RepadShape, new List<SelectListItem>
                        {
                            new SelectListItem { Text =  "SW+", Value = "SW+", Selected = true},
                            new SelectListItem { Text =  "SW-", Value = "SW-"},
                            new SelectListItem { Text =  "CATWALK", Value = "CATWALK"},
                            new SelectListItem { Text =  "CAGED LADDER", Value = "CAGED LADDER"},
                            new SelectListItem { Text =  "LADDER", Value = "LADDER"},
                        }, 
             new { @class = "form-control " })
            </div>
        </div>
        <h4>Miscellaneous</h4>
        <div class="form-group">
            @Html.LabelFor(model => model.DrawCircleorNot, "Draw Pipe Circle?", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.DrawCircleorNot, new List<SelectListItem>
                        {
                            new SelectListItem { Text =  "CIRCLE", Value = "CIRCLE"},
                            new SelectListItem { Text =  "NO CIRCLE", Value = "NO CIRCLE", Selected = true},
                        },
             new { @class = "form-control " })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Label, "Label?", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.DropDownListFor(model => model.Label, new List<SelectListItem>
                        {
                            new SelectListItem { Text =  "LABEL", Value = "LABEL", Selected = true},
                            new SelectListItem { Text =  "NO LABEL", Value = "NO LABEL"},
                        },
             new { @class = "form-control " })
            </div>
        </div>
        <hr />
        <div class="form-group">
            @Html.Label("Image of Shell Appurtenance", htmlAttributes: new { @class = "control-label col-md-4" })
            <div class="col-md-8">
                @Html.TextBoxFor(a=> a.Photo, new { type = "file", accept = "image/*", capture = "camera", @class = "btn  btn-default btn-sm" })
                <input name="uploadefile" type="file" accept="image/*" capture />
            </div>
        </div>
    </div>
    @Html.HiddenFor(h => h.Tank.Height, new { @id = "TankHeight" })

</div>

据我所知,发布的文件的命名约定应该是正确的,所以我不确定为什么它仍然返回 null。任何帮助或建议将不胜感激!

HttpPostfile 在尝试上传图像时返回空值

方法参数名称应与输入名称匹配。在您的代码中,两者并不相同。

因此,将剃须刀代码更改为

<input name="uploadedfile" type="file"/>