上传图像,重命名,缩略图,并替换原来的。 (优化) [英] Upload image, rename it, make thumbnail and replace the original. (optimization)

查看:137
本文介绍了上传图像,重命名,缩略图,并替换原来的。 (优化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了我在问题中描述的这些功能。然而,我认为我做这件事的方式并不是最好的做法。

  [HttpPost] 
public ActionResult创建(FormCollection集合,string schooljaarparam,FlatONASAanbieder foa){

if(ModelState.IsValid){

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

foreach(Request.Files中的字符串文件){
HttpPostedFileBase hpf = Request.Files [file] as HttpPostedFileBase;
if(hpf.ContentLength == 0)
continue;

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


字符串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)){
//大小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);


}其他{
ModelState.AddModelError(ONAS_Logo,这里是最好的和最好的。


$ b //r.Add(new ViewDataUploadFilesResult(){
// Name = savedFileName,
//长度= hpf.ContentLength
//});
}
}

//返回视图(UploadedFiles,r);

return View();


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

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

$ / code>

in 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;
}
返回false;
}

所以我这样做:


  1. 我从浏览器中获取文件

  2. 我检查它是否是图像

  3. 检查文件是否存在,如果是,请相应地更改文件名。

  4. 我将文件保存在磁盘上(IO,慢)
  5. 我打开文件作为图像
  6. 我使用VerkleinMaxHoogte方法计算宽度和高度
  7. 创建缩略图并用tmp扩展名保存
  8. >
  9. 我删除原始文件

  10. 我将缩略图重命名为原始文件名(这是我想要的)
  11. ol>

    我该如何做得更快?

    解决方案

    a href =http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream.aspx =nofollow noreferrer> HttpPostedFile.InputStream 和 Image.FromStream 方法来结合#4& #5。这也将消除#8& #9。


    I have created these functions which I described in the question. However I think the way I did it is not the optimal way of doing it.

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

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

    so the way I do it:

    1. I get the file from the browser
    2. I check if it is an Image
    3. I check if the file exists, and if so, change the filename accordingly
    4. I save the file on disk (IO, slow)
    5. I open the file as an image
    6. I calculate the width and height with the VerkleinMaxHoogte method
    7. I create the thumbnail and save it with a tmp extension
    8. I delete the original file
    9. I rename the thumbnail to the original file name (this is what I want)

    How do I do it faster?

    解决方案

    You can always use HttpPostedFile.InputStream and Image.FromStream method to combine #4 & #5. This will also eliminate #8 & #9.

    这篇关于上传图像,重命名,缩略图,并替换原来的。 (优化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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