Html-Code.BeginForm multipart/form-data Datei-Upload-Formular-group validation

Ich habe eine cshtml-Datei, um Dateien auf den server Hochzuladen.

@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary();
    <div class="alert alert-success alert-dismissible" role="alert">@ViewBag.Message</div>

        <div class="form-horizontal">
        <h4>Upload Data Documents</h4>
        <hr />
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.project_id)

        <div class="form-group">
            <label class="control-label col-md-2">Some Other File</label>
            <div class="col-md-10">
                <input type="file" name="someOtherFile" class="form-control" />
                <span class="field-validation-error" id="spanfilesomeOtherFile"></span>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-2">Results Comparison</label>
            <div class="col-md-10">
                <div class="form-group">
                    <div class="col-md-4">
                        <input type="file" name="FileUploadResultsComparison" class="form-control" placeholder=".col-md-4"/>
                        <span class="field-validation-error" id="spanfileResultsComparison"></span>
                    </div>
                    <div class="col-md-4">
                        @if (ViewData["Project"] != null)
                        { 
                            @Html.DropDownList("resultsComp_project", (SelectList)ViewData["Project"], "Select a Project", new { @class = "form-control", @placeholder = ".col-md-4" })
                        }
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-2">Memory Usage</label>
            <div class="col-md-10">
                <div class="form-group">
                    <div class="col-md-4">
                        <input type="file" name="FileUploadMemoryUsage" class="form-control" />
                        <span class="field-validation-error" id="spanfileMemoryUsage"></span>
                    </div>
                    <div class="col-md-4">
                        @if (ViewData["Project"] != null)
                        {
                            @Html.DropDownList("memUsage_project", (SelectList)ViewData["Project"], "Select a Project", new { @class = "form-control", @placeholder = ".col-md-4" })
                        }
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" name="Submit" id="btnSubmit" value="Upload Files" class="btn btn-default" />
            </div>
        </div>
}

Und mein Controller ist wie folgt

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file)
{
    foreach (string upload in Request.Files)
    {
        if (!(Request.Files[upload] != null && Request.Files[upload].ContentLength > 0)) continue;

        HttpPostedFileBase file = Request.Files[upload];

        if (ModelState.IsValid)
        {
            if (file == null)
            {
                ModelState.AddModelError("File", "Please Upload Your file");
            }
            else if (file.ContentLength > 0)
            {
                int MaxContentLength = 1024 * 1024 * 3; //3 MB
                string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".pdf" };

                if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
                {
                    ModelState.AddModelError("File", "Please file of type: " + string.Join(", ", AllowedFileExtensions));
                }

                else if (file.ContentLength > MaxContentLength)
                {
                    ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB");
                }
                else
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);
                    file.SaveAs(path);
                    ModelState.Clear();
                    ViewBag.Message = "File uploaded successfully";
                }
            }
        }
    }
    return View();
}

Den code oben funktioniert, einschließlich der Datei-Validierung. Aber ich habe ein Problem mit 2 Sachen in Bezug auf das "Projekt" in Verbindung mit der Datei.

  1. Wie ich den send/return des Projekts(id), die mit jeder Datei in den controller?
  2. Wie kann ich selektiv validieren, ob die "Projekt" - DropDown-Auswahl für die Datei?
    zum Beispiel (wenn ich auf durchsuchen, und wählen Sie eine Datei wie kann ich sicherstellen, dass eine entsprechende "resultsComp_project" Wert ausgewählt wird, in der Dropdown -? )

  3. wie Verbinde ich die Datei-und Projektverwaltung, wenn Sie ausgewählt werden, für beide (ich.e, name="FileUploadMemoryUsage" & name="FileUploadResultsComparison")

Bitte beachten Sie, dass die Informationen über Speicherbedarf kann auf null gesetzt werden, code wird nur eine Datei & Projekt-wenn Sie eingegeben werden.

InformationsquelleAutor You Rule Avi | 2014-11-18

Schreibe einen Kommentar