用于上传图像的进度条mvc

时间:2013-06-07 07:29:22

标签: asp.net-mvc file-upload dialog compression progress-bar

我有一个MVC asp.net应用程序,可以在我的应用程序中的文件夹中上传多个图像。它只是上传图像,压缩并将其保存到文件夹并将其缩略图保存到另一个文件夹。上传发生在后台,因为我可以在我的应用程序中执行其他工作。

上传大量图片需要一些时间。所以我需要在浮动对话框或弹出窗口中显示上传图像的状态作为进度条,当上传完成后我应该得到一个警报告知过程已经完成。

在视图中

<form action="" method="post" enctype="multipart/form-data">
  @Html.Label("Select the property : ");
  @Html.DropDownList("Address", new SelectList(ViewBag.Address as System.Collections.IEnumerable), "---Select---",new { id = "add"})

  <label for="file">Filename:</label>
  <input type="file" name="files" id="file" multiple="true"/>
  <input type="submit" />

在控制器

 [HttpPost]
        public ActionResult Index(HttpPostedFileBase[] files, FormCollection c)
        {
            int no = 0;
            string path = Path.Combine(Server.MapPath("~/Images"), c["Address"].Replace(' ', '_'));
            string fn = "";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            else
                no = Directory.GetFiles(path).Length;
            foreach (var file in files)
            {
                string fname = "img_" + no++ + file.FileName.Substring(file.FileName.LastIndexOf('.'));
                fn = Path.Combine(path, fname);
                Thread.Sleep(10000);

                ImageCodecInfo jpgEncoder = null;
                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                foreach (ImageCodecInfo codec in codecs)
                {
                    if (codec.FormatID == ImageFormat.Jpeg.Guid)
                    {
                        jpgEncoder = codec;
                        break;
                    }
                }
                var image1 = System.Drawing.Image.FromStream(file.InputStream, true, true);
                if (jpgEncoder != null)
                {
                    Encoder encoder = Encoder.Quality;
                    EncoderParameters encoderParameters = new EncoderParameters(1);

                        long quality = 20;
                        EncoderParameter encoderParameter = new EncoderParameter(encoder, quality);
                        encoderParameters.Param[0] = encoderParameter;
                        FileStream ms = new FileStream(fn, FileMode.Create, FileAccess.Write);
                        image1.Save(ms, jpgEncoder, encoderParameters);
                        ms.Flush();
                        ms.Close();
                }



                using (var image = System.Drawing.Image.FromStream(file.InputStream, true, true)) /* Creates Image from specified data stream */
                {
                    using (var thumb = image.GetThumbnailImage(
                         50, /* width*/
                         60, /* height*/
                         () => false,
                         IntPtr.Zero))
                    {
                        var jpgInfo = ImageCodecInfo.GetImageEncoders().Where(codecInfo => codecInfo.MimeType == "image/png").First(); /* Returns array of image encoder objects built into GDI+ */
                        using (var encParams = new EncoderParameters(1))
                        {
                            string pa = Path.Combine(path, "Thumb");
                            if (!Directory.Exists(pa))
                            {
                                Directory.CreateDirectory(pa);
                            }
                            string outputPath = Path.Combine(pa, fname);
                            long quality = 100;
                            encParams.Param[0] = new EncoderParameter(Encoder.Quality, quality);
                            thumb.Save(outputPath, jpgInfo, encParams);
                        }
                    }
                }

            }

            return RedirectToAction("Index");

}

任何人都可以帮助我在我的应用程序的浮动对话框或弹出窗口中创建进度条吗?

0 个答案:

没有答案