将2个不同的文件上传到表和文件夹中的2个不同的目的地 [英] upload 2 different files to 2 different destinations in table and folders

查看:136
本文介绍了将2个不同的文件上传到表和文件夹中的2个不同的目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,要在我的Db表中发布属于2个不同值的2个不同文件.

I have a form that I'm posting 2 different files that belongs to 2 different values in my Db table.

例如. file1=user imagefile2=user company logo.

所以我需要用viewModel将文件url附加到它的db值, 像这样的东西:(永远不会工作)

So I need to attach the files url to it's db value with my viewModel, something like this:(Will never work)

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

我的ViewModel(我有删除参数)

My ViewModel (I have remove parameters )

 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)
{

推荐答案

您可以使用IEnumerable<HttpPostedFileBase>通过以下代码轻松完成此操作:

You can do this easily with the below code using 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中,最后显示在视图中,请执行以下操作,对于图像上传,您显示的代码将起作用:

Note: Don't worry about image path. For the ease, just save the file name in the Db and finally to display in the view, do the following and for the image upload, the code you showed will work:

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

这篇关于将2个不同的文件上传到表和文件夹中的2个不同的目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆