文件“xxx”正由另一进程使用,因此该进程无法访问此文件。解决思路

文件“xxx”正由另一进程使用,因此该进程无法访问此文件。
使用uploadify3.2做了一个图片批量上传的功能,上传后图片使用dom加载出来,并添加了删除图片的方法。
在删除最后一张上传的图片时,提示: 文件“D:\wwwroot\feicuidd\uploads\130885024340197086.jpg”正由另一进程使用,因此该进程无法访问此文件。这肯定是某个对象没有释放引起的,但实在检查不出是哪个对象没有被释放,麻烦高手帮我看看。

try
        {
            System.IO.Stream stream = null;
            System.Drawing.Image originalImg = null;   //原图
            System.Drawing.Image thumbImg = null;      //缩放图
            
            int minWidth = 800;   //最小宽度
            int minHeight = 800;  //最小高度
            int maxWidth = 1440;  //最大宽度
            int maxHeight = 1440;  //最大高度

            string resultTip = string.Empty;  //返回信息
            HttpPostedFile file = context.Request.Files["Filedata"];      //上传文件
            string uploadPath = HttpContext.Current.Server.MapPath(@"/uploads");  //得到上传路径
            if (file != null)
            {
                if (!System.IO.Directory.Exists(uploadPath))
                {
                    System.IO.Directory.CreateDirectory(uploadPath);
                }
                string ext = System.IO.Path.GetExtension(file.FileName).ToLower();   //上传文件的后缀(小写)
                if (ext == ".jpg" || ext == ".png")
                {
                    string flag = DateTime.Now.ToFileTime() + ext;
                    string uploadFilePath = uploadPath + "/" + flag;   //缩放图文件路径
                    stream = file.InputStream;
                    //图片压缩
                    ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
                    Encoder ecd = Encoder.Quality;
                    EncoderParameters eptS = new EncoderParameters(1);
                    EncoderParameter ept = new EncoderParameter(ecd, 95L);
                    eptS.Param[0] = ept;
                    
                    originalImg = System.Drawing.Image.FromStream(stream);
                    
                    
                    
                    if (originalImg.Width > maxWidth && originalImg.Height > maxHeight)
                    {
                        thumbImg = PubClass.GetCutImage(originalImg, maxWidth, maxHeight);
                        thumbImg.Save(uploadFilePath, ici, eptS);
                        GC.Collect();
                        weight = 5;
                    }
                    else if ((originalImg.Width > minWidth && originalImg.Height > minHeight) && (originalImg.Width < maxWidth | originalImg.Height < maxHeight))
                    {
                        thumbImg = PubClass.GetThumbNailImage(originalImg, maxWidth, maxHeight);
                        thumbImg.Save(uploadFilePath, ici, eptS);
                        GC.Collect();
                        if(originalImg.Width > 1280 | originalImg.Height > 1280){
                            weight = 4;
                        }else if(originalImg.Width < 1280 && originalImg.Height < 1280){
                            weight = 3;
                        }
                    }
                    else
                    {
                        resultTip = "图片尺寸必须大于" + minWidth + "*" + minHeight + ",图片" + file.FileName + "上传失败。";
                        return;
                    }
                    //ici.
                    stream.Close();
                    stream.Dispose();
                    originalImg.Dispose();
                    thumbImg.Dispose();
                    GC.Collect();
                    //图片指纹
                    imghash getHash = new imghash(uploadFilePath);

                    using (feicuiddEntities fcdb = new feicuiddEntities())
                    {
                        int imgCount = fcdb.productImg.Count(s => s.pID == pid);
                        if (imgCount < 9)
                        {
                            //添加图片
                            productImg adimg = new productImg();
                            adimg.pID = pid;
                            adimg.picAddr = flag;
                            adimg.hushCode = getHash.GetHash();
                            adimg.weight = weight;
                            if (imgCount == 0)
                                adimg.isMainPic = true;
                            else
                                adimg.isMainPic = false;
                            fcdb.productImg.Add(adimg);
                            //更新主图及权重
                            product uppro = fcdb.product.Single(s => s.id == pid);
                            if (imgCount == 0)
                            {
                                uppro.mainPic = flag;
                            }
                            uppro.weight += weight;
                            fcdb.SaveChanges();

                            resultTip = "0|/uploads/" + flag + "|" + adimg.id + "|" + adimg.isMainPic;
                        }
                        else
                        {
                            resultTip = "已经超出9张图片。";
                        }
                        
                    }
                    
                }
            }
            else
            {
                resultTip = "上传文件为空";
            }

            context.Response.Write(resultTip);
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            GC.Collect();
        }


十分感谢了!
------解决思路----------------------
关键是你要采取行动去深入异步   -->   关键是你要采取行动去深入一步

这个时候你应该写一个测试,然后让你的测试保证能够出错。不会制造问题的人,往往也不会解决问题。
------解决思路----------------------
引用:
关键是你要采取行动去深入异步   -->   关键是你要采取行动去深入一步

这个时候你应该写一个测试,然后让你的测试保证能够出错。不会制造问题的人,往往也不会解决问题。


赞“不会制造问题的人,往往也不会解决问题。”