简体   繁体   中英

File upload control error-Access denied used by another person

when upload the same file for the multiple times i am getting this error......

"The process cannot access the file 'd:\MarketingSystem\ExcelImport\Sample.xls' because it is being used by another process."

getting error in this line

  RevenueDumpFileUpload.PostedFile.SaveAs(Server.MapPath(strFilePathOnServer) + RevenueDumpFileUpload.FileName);

This is my full code.....

 protected void btnImport_Click(object sender, EventArgs e)
{
    if (RevenueDumpFileUpload.HasFile)
    {

        string strFilePathOnServer = ConfigurationManager.AppSettings["RevenueDumpFileLocation"];
        String sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(strFilePathOnServer) + RevenueDumpFileUpload.FileName + ";Extended Properties=Excel 8.0;";
        string strPostedFileName = RevenueDumpFileUpload.PostedFile.FileName;
        if (strPostedFileName != string.Empty && RevenueDumpFileUpload.PostedFile.ContentLength != 0)
        {
            //Delete Old file before uploading new file. 
            if (System.IO.File.Exists(strFilePathOnServer))
            {
                System.IO.File.Delete(strFilePathOnServer);

            }
            //Save-Upload File to server. 
            RevenueDumpFileUpload.PostedFile.SaveAs(Server.MapPath(strFilePathOnServer) + RevenueDumpFileUpload.FileName);
            RevenueDumpFileUpload.FileContent.Dispose();
        }
        OleDbConnection Exlcon = new OleDbConnection(sConnectionString);
        try
        {
            Exlcon.Open();
        }
        catch
        {
            return;
        }
        finally
        {
            RevenueDumpFileUpload.PostedFile.InputStream.Flush();
            RevenueDumpFileUpload.PostedFile.InputStream.Close();
        }

        OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", Exlcon);
        OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
        objAdapter1.SelectCommand = objCmdSelect;
        objAdapter1.Fill(objDataset1, "XLData");
        methodtosave();

    }

}

In my web config file:

    <appSettings>
<add key="RevenueDumpFileLocation" value="~/ExcelImport/"/>

How to resolve this?

Help me..

Thanks in advance

Well, if the OleDbConnection acts anything like the SqlConnection object, you've got this line:

Exlcon.Open();

which is opening the connection, but you don't have a matching line to close the connection. Which means the Jet database provider is going to continue to keep this file open until the connection object is garbage collected. It would be far better to wrap this line:

OleDbConnection Exlcon = new OleDbConnection(sConnectionString);

In a using statement, whose body extends over the remainder of the function, so that you're guaranteed that it's closed/disposed.

Next, have you considered what happens if multiple users upload files with the same name simultaneously - this method will be broken. It may be better to use a new file name on the server, related to the user ID or session ID, and wrap a try/finally around the whole method to ensure the file is deleted after use.

The above may be the cause of your current issues, if this is an error coming out of production - if two people attempt an upload at the same time, then both of their requests may go past the "delete if it exists" part of the code, then one request manages to save the file and open a connection, then the other request will fall over when trying to save the same file name.

You forget to pass the File Name and File Extension when you are trying to delete the file.

if (System.IO.File.Exists(Server.MapPath(strFilePathOnServer) + strPostedFileName+ 
    System.IO.Path.GetExtension(RevenueDumpFileUpload.FileName)))
{
   System.IO.File.Delete(Server.MapPath(strFilePathOnServer) + strPostedFileName +
   System.IO.Path.GetExtension(RevenueDumpFileUpload.FileName) );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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