【问题标题】:Upload image, rename it, make thumbnail and replace the original. (optimization)上传图片,重命名,制作缩略图并替换原图。 (优化)
【发布时间】:2010-08-30 09:31:03
【问题描述】:

我已经创建了我在问题中描述的这些功能。但是我认为我这样做的方式并不是最佳的方式。

        [HttpPost]
        public ActionResult Create(FormCollection collection, string schooljaarparam, FlatONASAanbieder foa) {

        if (ModelState.IsValid) {

            // var r = new List<ViewDataUploadFilesResult>();

            foreach (string file in Request.Files) {
                HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf.ContentLength == 0)
                    continue;

                //extensie nakijken. jpg, png, jpeg, of GIF. 
                if (MvcApplication.isImage(hpf.FileName)) {
                    //Image img = new Image();


                    string savedFileName = Path.Combine(
                       AppDomain.CurrentDomain.BaseDirectory + "uploads\\ONAS\\",
                       Path.GetFileName(hpf.FileName));
                    FileInfo fi = new FileInfo(savedFileName);

                    int i = 1;
                    while (fi.Exists) {
                        fi = new FileInfo(savedFileName.Substring(0, savedFileName.Length - Path.GetFileName(savedFileName).Length) + Path.GetFileNameWithoutExtension(savedFileName) + " (" + i++ + ") " + Path.GetExtension(savedFileName));
                    }
                    savedFileName = fi.DirectoryName + "\\" + fi.Name;
                    hpf.SaveAs(savedFileName);

                    using (Image Img = Image.FromFile(savedFileName)) {
                        //Size ThumbNailSize = NewImageSize(Img.Height, Img.Width, 79);
                        Size NewSize = VerkleinMaxHoogte(Img.Size, 79);

                        using (Image ImgThnail = new Bitmap(Img, NewSize.Width, NewSize.Height)) {
                            //string ss = savedFileName.Substring(0, savedFileName.Length - Path.GetFileName(savedFileName).Length) + Path.GetFileNameWithoutExtension(savedFileName) + "-thumb" + Path.GetExtension(savedFileName);
                            ImgThnail.Save(savedFileName + ".tmp", Img.RawFormat);
                            ImgThnail.Dispose();
                        }
                        Img.Dispose();
                    }
                    System.IO.File.Delete(savedFileName);
                    FileInfo f = new FileInfo(savedFileName + ".tmp");
                    f.MoveTo(savedFileName);


                } else {
                    ModelState.AddModelError("ONAS_Logo", "Het geuploadde bestand is geen afbeelding. ");

                }

                //r.Add(new ViewDataUploadFilesResult() {
                //    Name = savedFileName,
                //    Length = hpf.ContentLength
                //});
            }
        }

        // return View("UploadedFiles", r);

        return View();
    }


    [NonAction]
    public Size VerkleinMaxHoogte(Size orig, double height) {
        double tempval = height / orig.Height;

        return new Size(Convert.ToInt32(tempval * orig.Width), Convert.ToInt32(height));
    }

在 global.asax 中

    public static bool isImage(string s) {
        if (s.EndsWith(".jpg", true, null) || s.EndsWith(".jpeg", true, null) || s.EndsWith(".gif", true, null) || s.EndsWith(".png", true, null)) {
            return true;
        }
        return false;
    }

我就是这样做的:

  1. 我从浏览器获取文件
  2. 我检查它是否是图像
  3. 我检查文件是否存在,如果存在,相应地更改文件名
  4. 我将文件保存在磁盘上(IO,慢)
  5. 我将文件作为图像打开
  6. 我用 VerkleinMaxHoogte 方法计算宽度和高度
  7. 我创建缩略图并使用 tmp 扩展名保存它
  8. 我删除了原文件
  9. 我将缩略图重命名为原始文件名(这是我想要的)

如何更快地做到这一点?

【问题讨论】:

    标签: c# asp.net-mvc file-io file-upload


    【解决方案1】:

    您始终可以使用HttpPostedFile.InputStreamImage.FromStream 方法来组合#4 和#5。这也将消除#8 和#9。

    【讨论】:

    • 好点。我正在寻找类似的东西,但没有找到:)
    猜你喜欢
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 2013-05-31
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2015-09-22
    相关资源
    最近更新 更多