将上传的文件保存到服务器文件系统,并使用ASP.NET MVC5将文件路径保存在sql server数据库中

时间:2014-04-11 21:33:00

标签: sql-server asp.net-mvc

我正在开发一个ASP.NET MVC5应用程序,它在db.table上保存文件属性,同时将实际文件上传到文件夹中。它工作正常,但我不知道如何在上传文件时保存数据库中的文件路径。我想将上传的文件路径存储在数据库中,以便能够使用路径检索它。非常感谢!! 控制器如下:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "LearnObj_ID,LearnObj_No,LearnObj_Title,LearnObj_Description,LearnObj_Keyword,LearnObj_Language,LearnObj_CreatorName,LearnObj_CreatorLastName,LearnObj_Email,LearnObj_Version,LearnObj_Status,LearnObj_Date,LearnObj_Coverage,LearnObj_Contributor,LearnObj_Format,LearnObj_Location,LearnObj_Subject,LearnObj_Relation,LearnObj_Source,LearnObj_Publisher,LearnObj_Type")] Learning_Object learning_object, HttpPostedFileBase UploadTheFile)
    {

            if (UploadTheFile != null && UploadTheFile.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(UploadTheFile.FileName);
                // store the file inside ~/Content/LearnObject-Repository folder
                var path = Path.Combine(Server.MapPath("~/Content/LearnObject-Repository"), fileName);
                UploadTheFile.SaveAs(path);

                here, I want to save the file path(as above) in the db.Learning_Object  
                 the field is LearnObj_Source

            }


            if (ModelState.IsValid)
            {
                db.Learning_Object.Add(learning_object);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(learning_object);
        }
      }
   //-------   end of uploading ---->

视图模型如下:

    @model LMS.Models.Learning_Object

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


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

    <div class="form-horizontal">
        <h4>Learning_Object</h4>
        <hr />

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_No, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_No)
                @Html.ValidationMessageFor(model => model.LearnObj_No)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Title, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Title)
                @Html.ValidationMessageFor(model => model.LearnObj_Title)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Description, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Description)
                @Html.ValidationMessageFor(model => model.LearnObj_Description)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Keyword, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Keyword)
                @Html.ValidationMessageFor(model => model.LearnObj_Keyword)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Language, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Language)
                @Html.ValidationMessageFor(model => model.LearnObj_Language)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_CreatorName, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_CreatorName)
                @Html.ValidationMessageFor(model => model.LearnObj_CreatorName)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_CreatorLastName, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_CreatorLastName)
                @Html.ValidationMessageFor(model => model.LearnObj_CreatorLastName)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Email, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Email)
                @Html.ValidationMessageFor(model => model.LearnObj_Email)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Version, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Version)
                @Html.ValidationMessageFor(model => model.LearnObj_Version)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Status, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Status)
                @Html.ValidationMessageFor(model => model.LearnObj_Status)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Date, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Date)
                @Html.ValidationMessageFor(model => model.LearnObj_Date)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Coverage, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Coverage)
                @Html.ValidationMessageFor(model => model.LearnObj_Coverage)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Contributor, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Contributor)
                @Html.ValidationMessageFor(model => model.LearnObj_Contributor)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Format, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Format)
                @Html.ValidationMessageFor(model => model.LearnObj_Format)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Location, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Location)
                @Html.ValidationMessageFor(model => model.LearnObj_Location)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Subject, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Subject)
                @Html.ValidationMessageFor(model => model.LearnObj_Subject)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Relation, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Relation)
                @Html.ValidationMessageFor(model => model.LearnObj_Relation)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Source, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Source)
                @Html.ValidationMessageFor(model => model.LearnObj_Source)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Publisher, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Publisher)
                @Html.ValidationMessageFor(model => model.LearnObj_Publisher)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LearnObj_Type, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LearnObj_Type)
                @Html.ValidationMessageFor(model => model.LearnObj_Type)
            </div>
        </div>

        <div>
            <div> 
            <input type="file" name="UploadTheFile" value="UploadTheFile"/>
           </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>

    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

2 个答案:

答案 0 :(得分:0)

因为您的数据库不应该依赖于服务器路径,所以可以将此部分保存为路径

var fileNameToSaveInDB = @"Content/LearnObject-Repository/"+fileName

然后,只要你想加载文件,你就可以这样做

Path.Combine(Server.MapPath("~",Table.FileNameField)

答案 1 :(得分:0)

你不能在db.Learning_Object表中添加一个nvarchar列并将路径保存为文本吗? 你可以像这样制作一个存储过程

@path nvarchar(MAX)
as
INSERT INTO db.Learning_Object (path,column2,column3,...)
VALUES (@path,value2,value3,...)

然后(如果你使用linq)调用存储过程并插入你想要保存的路径。

LearningObjectDataContextClass db = new LearningObjectDataContextClass();
db.InsertPath(pathtoinsert);