简体   繁体   English

HttpPostedFileBase锁定文件

[英]HttpPostedFileBase locking file

I am uploading a file using HttpPostedFileBase . 我正在使用HttpPostedFileBase上传文件。 The file uploads without an issue, but when I try and read from that file (excel file), I get an error saying that I can not read from the file as it is locked. 该文件上传没有问题,但是当我尝试从该文件(excel文件)读取时,出现错误消息,因为该文件已锁定,因此无法读取。 How do I remove the lock from the file / make it not lock the file in the first place? 如何从文件中删除锁定/使其一开始不锁定文件?

[HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            string path = string.Empty;
            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                path = Path.Combine(Server.MapPath("~/App_Data/"), Guid.NewGuid() + fileName);
                file.SaveAs(path);
                file.InputStream.Flush(); 

                file.InputStream.Close();
                file.InputStream.Dispose();

                // Import the file
                ViewData["FileName"] = fileName;
                //Added to ensure that the file had finished being written by the time      I try and read it
                System.Threading.Thread.Sleep(2000);
            }

            return RedirectToAction("Index", new{fileName = path});
        }

The above code reads the file and saves it without issue, but the below fails to then load that file: 上面的代码读取文件并将其保存,没有任何问题,但是下面的代码无法随后加载该文件:

var workbook = excelApp.Workbooks.Open(ExcelDocumentLocation, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); var workbook = excelApp.Workbooks.Open(ExcelDocumentLocation,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value ,Missing.Value,Missing.Value,Missing.Value,Missing.Value);

I have confirmed that it is the fact that the file is locked as the issue by opening the file in excel and seeing the "File is locked by another user" message. 我已经确认,通过在excel中打开文件并看到“文件已被另一个用户锁定”消息,该文件已被锁定为问题。

All you need is file.SaveAs(path); 您只需要file.SaveAs(path); . You don't need to flush or close the file.InputStream . 您不需要刷新或关闭file.InputStream This will not lock the file. 不会锁定文件。

It is the code that is reading from it that is locking it. 锁定它的是从中读取的代码。 I see that you are using Office Interop in your ASP.NET MVC application. 我看到您在ASP.NET MVC应用程序中使用Office Interop。 Don't use it. 不要使用它。 This was never intended to be used in a multithreaded environment such as an ASP.NET application. 从未打算将其用于多线程环境(例如ASP.NET应用程序)中。 The thing with Office Interop is that you actually need to have MS Office installed on the server which was never meant to be used that way. Office Interop的问题在于,您实际上需要在服务器上安装MS Office,而该方式永远都不会被使用。 It's the MS Office process that is locking the file, not your ASP.NET MVC application that simply stored it on the disk. 锁定文件的是MS Office进程,而不是简单地将其存储在磁盘上的ASP.NET MVC应用程序。

If you want to manipulate Office documents on the server you could use the OpenXML SDK . 如果要在服务器上处理Office文档,则可以使用OpenXML SDK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM