将文件上传到所选文件夹

时间:2015-06-11 13:19:35

标签: asp.net-mvc-4 c#-4.0 entity-framework-5

我有带文件夹名称的单选按钮。用户可以选择他/她想要上传文件的文件夹名称。然后他/她选择文件夹名称并上传文件

这是模型:

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;


        }
    }

这个方法:

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

这是观点:

@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")
}

谢谢

如果我现在尝试上传,我会收到此错误:

对象引用未设置为对象的实例。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息。

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

来源错误:

第746行:{ 第747行:TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId,customerSchema); 第748行:var path = Path.Combine(Server.MapPath(&#34;〜/&#34;),entry.FilePath); 749行: 第750行:foreach(uploadViewModel中的var项目)

源文件:b:\ Seneca \ Producten \ FormsServer \ Trunk \ SenecaFormsServer \ Areas \ Dashboard \ Controllers \ DesignTemplateController.cs

0 个答案:

没有答案