2016-09-26 67 views
0

我有一個表單,我發佈了2個不同的文件,屬於我的Db表中的2個不同的值。將2個不同的文件上傳到表和文件夾中的2個不同的目的地

例如, file1=user imagefile2=user company logo

所以我需要附加文件的URL到它的分貝值與我viewModel, 是這樣的:(行不通)

public ActionResult Create(LectureFormViewModel viewModel) 
    { 
     foreach ((string item in Request.Files).viewModel.Image1) 
     { 
      //Do 
     } 
      foreach ((string item in Request.Files).viewModel.Image2) 
      { 
       //Do 
      } 
     var lecture = new Lecture 
     { 
      Image1 = xxx, 
      Image2=yyy, 
     } 
     _context.LectureGigs.Add(Lecture); 
    } 

我的視圖模型(我已經刪除參數)

public class LectureFormViewModel 
{ 
    public int Id { get; set; } 
    public byte Genre { get; set; } 
    public IEnumerable<Genre> Genres { get; set; } 

    public string Image1 { get; set; } 


    public string Image2 { get; set; } 

    public string Action 
    { 
     get 
     { 
      Expression<Func<LecController, ActionResult>> 
       update = (c => c.Update(this)); 

      Expression<Func<LecController, ActionResult>> 
       create = (c => c.Create(this)); 

      var action = (Id != 0) ? update : create; 
      return (action.Body as MethodCallExpression).Method.Name; 
     } 

    } 
} 

形式(查看)

@using VoosUpW.Models 
@model VoosUpW.ViewModels.LectureFormViewModel 

@using (Html.BeginForm(Model.Action, "Lec", FormMethod.Post, new { enctype = "multipart/form-data", @id = "abcdefg" })) 

{ 
    //parm 

    <div class="form-group"> 
     @Html.LabelFor(f => f.Image1) 
     <i class="glyphicon glyphicon-folder-open"></i> 
     <input id="Image1" name="Image" type="file" class=""> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(f => f.Image2) <i class="glyphicon glyphicon-folder-open"></i> 
     <input type="file" name="Image2" class="btn btn-default btn-sm btn-google btn-group-justified hvr-shadow " /> 
    </div> 
     <button type="submit" class="btn btn-primary btn-lg">Save</button> 

} 

我的老天行動明鏡

public ActionResult Create(LectureFormViewModel viewModel) 
{ 
+0

我什麼都看不到上傳圖像或將數據保存在數據庫中。你是什​​麼意思的文件網址?圖像可以用文件名或字節數組保存。 –

+0

string savedFileName = System.IO.Path.GetFileName(file.FileName); var newFileName = DateTime.Now.ToString(「yyMMddmmss」)+ savedFileName; if(System.IO.File.Exists(path))continue; MyFileName =(「/ Images/RImages /」+ newFileName); & newFileName = Server.MapPath(MyFileName); file.SaveAs(newFileName); &然後Image1 = MyfileName;和「添加保存」等。 –

+0

您已將其硬編碼。我將提供兩個文件上傳的示例。這很簡單。 –

回答

0

可以使用IEnumerable<HttpPostedFileBase>下面的代碼做到這一點很容易:

產品:

public class Product 
{ 
    public int ProductId { get; set; } 
    public string ProductName { get; set; } 
    public string ImagePath1 { get; set; } 
    public string ImagePath2 { get; set; } 
    public double Price { get; set; } 
} 

行動:

[HttpPost] 
public ActionResult Upload(Product aProduct, IEnumerable<HttpPostedFileBase> files) 
{ 
    HttpPostedFileBase file1; 
    HttpPostedFileBase file2; 

    using (var context = new DemoEntities()) //DbContext - Database connection 
    { 
     file1 = files.ElementAt(0); //Gets the first image 
     file2 = files.ElementAt(1); //Gets the second image 

     //Take the first and second file 
     if (files.ElementAt(0) != null && files.ElementAt(1) != null) 
     { 
     if (file1 != null && file1.ContentLength > 0 && file2 != null && file2.ContentLength > 0) 
     { 
      var fileName = System.IO.Path.GetFileName(file1.FileName); 
      string path = System.IO.Path.Combine(Server.MapPath("~/Images/"), fileName); //Get the first file path and save to folder 

      var fileName2 = System.IO.Path.GetFileName(file2.FileName); 
      string path2 = System.IO.Path.Combine(Server.MapPath("~/Images2/"), fileName2); //Get the second file path and save to folder 

      /**In database, followings are being saved - Starts**/ 
      aProduct.ProductId = 1002; 
      aProduct.ImagePath1 = fileName; //Saves the first image file name 
      aProduct.ImagePath2 = fileName2; //Saves the second image file name 
      aProduct.ProductName = "Super Product"; 
      aProduct.Price = 2000; 

      try 
      { 
      context.Products.Add(aProduct); //Save the object and context is the dbContext 
      context.SaveChanges(); 
      } 

      catch (Exception ex) 
      { 
      ex.ToString(); 
      } 
      /**In database, followings are being saved - Ends**/ 
      } 
     } 
    } 

    return RedirectToAction("Index"); 
} 

注:不要擔心圖像路徑。爲方便,只需保存的文件名在DB終於在視圖中顯示,請執行下列操作以及圖片上傳,您顯示的代碼將工作:

<img src="~/Images1/@item.ImagePath1" alt="No Images" class="img"> 
<img src="~/Images2/@item.ImagePath2" alt="No Images" class="img"> 
+0

謝謝你的回答,但它不能與我的viewModel和 –

+0

模型工作的文件是空的,file1,file2是我的Vm的一部分 –

+0

這應該工作。我沒有明白你的意思。你能分享你的模型和視圖嗎? –

相關問題