IIS锁定上传文件的问题

时间:2011-05-03 20:57:54

标签: c# asp.net

我有一个小型上传应用程序,我使用webservice(asmx)上传文件,检查MD5并验证它。问题是,当我验证文件时,它说该文件被另一个进程锁定。以下是我上传和验证的代码:

private static object padlock = new object();

以较小的比例分组上传文件并上传每个文件

[WebMethod]
        public void LargeUpload(byte[] content, string uniqueName)
        {
            lock (padlock)
            {
                string path = Server.MapPath(PartialDir + "/" + uniqueName);
                BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Append, FileAccess.Write));
                writer.Write(content);
                writer.Flush();
                writer.Close();
                writer = null;
            }
        }

在最后一个之后将其插入我的数据库。在此之后,客户端通过请求MD5来验证文件:

[WebMethod]
        public int EndLargeUpload(string name, int folderId, long length, string uniqueName, int customerid)
        {
            lock (padlock)
            {
                string path = Server.MapPath(PartialDir + "/" + uniqueName);
                string newPath = Server.MapPath(RepositoryDir + "/" + uniqueName);
                File.Copy(path, newPath);
                //delete partial
                File.Delete(path);
                string extension = Path.GetExtension(uniqueName);
                string newFileName = uniqueName;
                GWFile newFile = new GWFile();
                newFile.DiscName = newFileName;
                newFile.FileName = name;
                newFile.FolderId = folderId;
                newFile.Description = "";
                newFile.Size = (int)length;
                newFile.DiscFolder = Server.MapPath("/Repository");
                newFile.DiscRelativePath = "/Repository/" + newFile.DiscName;
                newFile.CustomerId = customerid;
                IGWFileRepository fileRepository = ObjectFactory.GetInstance<IGWFileRepository>();


                fileRepository.SaveFile(newFile);
                return newFile.Id;
            }
        }

在EndLargeUpload()方法之后,客户端使用文件的id调用RequestMD5方法,此调用除外,它无法打开文件“..... xxx ...”,因为它正被使用通过另一个过程...

private string GetMD5HashFromFile(string fileName)
            {
                lock (padlock)
                {
                    using (FileStream file = new FileStream(fileName, FileMode.Open)) // <-- excepts here
                    {
                        MD5 md5 = new MD5CryptoServiceProvider();
                        byte[] retVal = md5.ComputeHash(file);
                        file.Close();

                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < retVal.Length; i++)
                        {
                            sb.Append(retVal[i].ToString("x2"));
                        }
                        return sb.ToString();
                    }
                }
            }

我使用sysinternals中的进程资源管理器来查看文件,它说该文件已被Web服务器锁定(请参阅此img:http://screencast.com/t/oqvqWXLjku) - Web服务器如何锁定它?我可以解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

EndLargeUpload方法的最后两行怎么样:

IGWFileRepository fileRepository = ObjectFactory.GetInstance<IGWFileRepository>();
fileRepository.SaveFile(newFile);

IGWFileRepository.SaveFile()是否可能没有正确关闭文件?

答案 1 :(得分:0)