Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:09:59+00:00 2026-06-12T13:09:59+00:00

I am uploading a file using HttpPostedFileBase . The file uploads without an issue,

  • 0

I am uploading a file using 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. 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);

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:10:00+00:00Added an answer on June 12, 2026 at 1:10 pm

    All you need is file.SaveAs(path);. You don’t need to flush or close the 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. Don’t use it. This was never intended to be used in a multithreaded environment such as an ASP.NET application. 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. It’s the MS Office process that is locking the file, not your ASP.NET MVC application that simply stored it on the disk.

    If you want to manipulate Office documents on the server you could use the OpenXML SDK.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Django app, I'm uploading a text file, using file.read() to get the
Using Struts 2: when will the .tmp file - that gets created after uploading
I'm implementing file uploading using Richfaces version 4.0. I've noticed, that there is quite
Actually I'm creating an application for uploading file using JSF. But whenever I upload
When uploading file, is that possible to get the create time of the file
I'm about uploading file to remote server using fsockopen() without using context. I'm able
I was uploading my file using below code,it works fine but some time it
I am currently looking in to some file uploading using Java Server Faces. I've
Hi am using apache commons upload for uploading files File file=this.getFile();//getter method for the
I am uploading a file in my ASP.NET MVC application using Uploadify. Controller: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.