MVC 3.0 上传多张图片到服务器

View关键代码:

@using (Html.BeginForm("Create", "Activity", FormMethod.Post, new { enctype = "multipart/form-data" }))
  <div class="controls ">
        <span class="signup">图片1:&nbsp;&nbsp;<input type="file" name="Image1" />
            @Html.TextBoxFor(model => model.Image1, new { @disabled = "disabled", @style = "width:300px;" })
            @Html.ValidationMessageFor(model => model.Image1)</span>
    </div> 
    <div class="controls ">
        <span class="signup">图片2:&nbsp;&nbsp;
            <input type="file" name="Image2" />
            @Html.TextBoxFor(model => model.Image2, new { @disabled = "disabled", @style = "width:300px;" })
            @Html.ValidationMessageFor(model => model.Image2)
        </span>
    </div>   
    <div class="controls ">
        <span class="signup">图片3:&nbsp;&nbsp;
            <input type="file" name="Image3" />
            @Html.TextBoxFor(model => model.Image3, new { @disabled = "disabled", @style = "width:300px;" })
            @Html.ValidationMessageFor(model => model.Image3)
        </span>
    </div>
    <p>
        <input type="submit" value="保存草稿" name="action" />
        <input type="submit" value="发布" name="action" />
    </p>  

Controller主要代码:

  [ValidateInput(false)]
        public ActionResult Create(string action, ActivityWeb.Models.activities model, string[] City)
        {
  foreach (string inputName in Request.Files.AllKeys)
                {
                    HttpPostedFileBase file = Request.Files[inputName];
                    //存入文件
                    if (file.ContentLength > 0)
                    {
                        //PropertyInfo pro = model.GetType().GetProperty(inputName);
                        //string proValue = (string)pro.GetValue(model, null);
                        //if (!string.IsNullOrWhiteSpace(proValue))
                        //{
                        //    Common.FileUpload.DeleteFile(proValue);

                        //}
                        string fileName = Common.FileUpload.UploadFile(file);
                       // pro.SetValue(model, fileName, null);
                    }
                }

 

public static class FileUpload
    {

        //public static char DirSeparator = Path.DirectorySeparatorChar;

        //public static string FilesPath = string.Format("ActivityFile{0}Images{0}{1}{0}{2}{3}{0}", DirSeparator, DateTime.Now.Year, DateTime.Now.Month.ToString().PadLeft(2, '0'), DateTime.Now.Day.ToString().PadLeft(2, '0'));
        //public static string PhysicalPath = HttpContext.Current.Server.MapPath(FilesPath);

        public static string UploadFile(HttpPostedFileBase file)
        {

            // Check if we have a file

            if (null == file) return "";

            // Make sure the file has content

            if (!(file.ContentLength > 0)) return "";

            string fileName = file.FileName;

            string fileExt = Path.GetExtension(file.FileName);

            // Make sure we were able to determine a proper

            // extension

            if (null == fileExt) return "";
            char DirSeparator = '/';
            string FilesPath = string.Format("{0}team{0}{1}{0}{2}{3}{0}", DirSeparator, DateTime.Now.Year, DateTime.Now.Month.ToString().PadLeft(2, '0'), DateTime.Now.Day.ToString().PadLeft(2, '0'));
            string PhysicalPath = HttpContext.Current.Server.MapPath(FilesPath);
            // Check if the directory we are saving to exists

            if (!Directory.Exists(PhysicalPath))
            {

                // If it doesn't exist, create the directory

                Directory.CreateDirectory(PhysicalPath);

            }

            // Set our full path for saving

            string path = PhysicalPath + fileName;

            // Save our file

            file.SaveAs(Path.GetFullPath(path));

            // Return the filename


            return FilesPath + fileName;

        }

        public static void DeleteFile(string fileName)
        {
            if (!string.IsNullOrWhiteSpace(fileName))
            {
                // Don't do anything if there is no name

                if (fileName.Length == 0) return;

                // Set our full path for deleting

                string path = HttpContext.Current.Server.MapPath(fileName);

                // Check if our file exists

                if (File.Exists(Path.GetFullPath(path)))
                {

                    // Delete our file

                    File.Delete(Path.GetFullPath(path));

                }
            }
        }

    }

 

转载于:https://www.cnblogs.com/LLLLoveLLLLife/p/3502308.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值