2014-04-11 47 views
-1

我正在研究保存db.table文件屬性的ASP.NET MVC5應用程序,並同時上傳文件夾內的實際文件。它工作正常,但我不知道如何在上傳文件時將我的文件路徑保存在數據庫中。我想將上傳的文件路徑存儲在數據庫中,以便能夠使用路徑來檢索它。非常感謝!! 控制器是:將上傳的文件保存到服務器文件系統,並使用ASP.NET MVC5在sql server數據庫中保存文件路徑

[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") 
} 
+0

也許它會幫助,如果你真的問了一個問題 –

+1

感謝您的答覆,我會編輯我的問題。 – user3521892

回答

0

因爲,你的數據庫不應該依賴於服務器的路徑,你可以這部分保存爲路徑

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

然後每當你想加載文件,你可以這樣做

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

Path.Combine(@「Content/LearnObject-Repository」,fileName)不起作用。我有var path = Path.Combine(Server.MapPath(「〜/ Content/LearnObject-Repository」),fileName);在我的控制器中。 – user3521892

+0

@ user3521892我已編輯我的答案 – RezaRahmati

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); 
相關問題