Upload file to chosen folder

45 views Asked by At

I have radio buttons with the folder name. A user can choose a folder name where he/she wants to upload a file. then he/she choose the folder name and upload the file

this is the model:

public class UploadViewModel
    {

        public string Id { get; set; }

        [Required]
        [Display(Name = "FormToUpload", ResourceType = typeof(Resources.Entity.Form))]
        public HttpPostedFileBase UploadData { get; set; }

        private UploadModel _uploadModel;

        public string[] Directories { get; set; }
        public bool? IsActive { get; set; }




        public UploadViewModel(UploadModel uploadModel)
        {
            _uploadModel = uploadModel;


        }
    }

this the method:

[HttpPost]
        [ValidateAntiForgeryToken]
        public  ActionResult UploadFile([Bind(Include = "UploadData")] LibraryUploadModel libraryUpload, string designId, string[] selectedFile)
        {
            TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId, customerSchema);
             var path = Path.Combine(Server.MapPath("~/"), entry.FilePath);

            if (Request != null)
            {
                //HttpPostedFileBase file = Request.Files["UploadData"];

                if ((libraryUpload.UploadData != null) && (libraryUpload.UploadData.ContentLength > 0) && !string.IsNullOrEmpty(libraryUpload.UploadData.FileName))
                {                   
                    var fileName = Path.GetFileName(libraryUpload.UploadData.FileName);
                    //var path = Path.Combine(Server.MapPath("~/img/Data"), fileName);
                    libraryUpload.UploadData.SaveAs(path);                    
                }
            }

            return View();
        }

and this is the view:

@model  SenecaFormsServer.Areas.Dashboard.Models.UploadViewModel
    ViewBag.Title = Resources.Entity.DesignTemplate.UploadForm;
}

@Html.Partial("~/Areas/_Shared/_BreadCrumbsPartial.cshtml")

<!-- widget grid -->
<section id="widget-grid">
    @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()

        <div class="row">
            <div class="col-xs-12 ">
                @Html.RenderNotifications()
            </div>

            <div class="col-xs-12 padding-bottom-10">
                <button type="submit" value="UploadFile" class="btn btn-success"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.Upload</button>
                <a href="@Url.Action("Index")" class="btn btn-danger"><i class="fa fa-fw fa-times"></i>@Resources.Action.Navigation.Cancel </a>
            </div>
        </div>

        <div class="well no-padding">
            <div class="bg-color-white">

                <div class="row padding-10">
                    <div class="col-xs-12">
                        <h4>@Resources.Entity.DesignTemplate.FileName</h4>
                    </div>

                    <div class="col-xs-12 margin-bottom-10 margin-top-10">
                        <div class="form-horizontal">
    @Html.ValidationSummary(true)

    @*<div class="form-group">
            @Html.LabelFor(model => model.UploadData, new { @class = "text-bold control-label col-md-2" })
            <div class="col-lg-6 col-md-8 col-sm-10">
                <input name="@Html.NameFor(model => model.UploadData)" type="file" />
                @Html.ValidationMessageFor(model => model.UploadData)
            </div>
        </div>*@





    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <table>
                @foreach (var item in Model.Directories)
                {
                    <tr>
                        <td>
                            @Html.RadioButton("Assigned", Model.IsActive.HasValue ? Model.IsActive : false);
                            @Html.Label(item)
                        </td>
                    </tr>
                }
            </table>
                        </div>
                        </div>

<div class="form-group">
    @Html.LabelFor(model => model.UploadData, new { @class = "text-bold control-label col-md-2" })
    <div class="col-lg-6 col-md-8 col-sm-10">
        <input name="@Html.NameFor(model => model.UploadData)" type="file" />
        @Html.ValidationMessageFor(model => model.UploadData)
    </div>
</div>




                        </div>
                        </div>

                    </div>
            </div>
        </div>

    }
</section>
<!-- end widget grid -->
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Thank you

If i now try to upload I get this error:

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 746: { Line 747: TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId, customerSchema); Line 748: var path = Path.Combine(Server.MapPath("~/"), entry.FilePath); Line 749: Line 750: foreach (var item in uploadViewModel)

Source File: b:\Seneca\Producten\FormsServer\Trunk\SenecaFormsServer\Areas\Dashboard\Controllers\DesignTemplateController.cs

0

There are 0 answers