Unable to delete the file saved on the server.
-
Respected Gurus I m using following code to import data from an excel file.. It works fine except one problem that whenever i m trying to delete the file which i have saved on the server by using file.delete(strfilename) i left with the error "you do not have permission" I think i have not closed the excel file. how can i close it programmatically so that i can delete it also. Dim strfilename As String ' Check the first open file dialog for a value If Not (Me.File1.PostedFile Is Nothing) Then ' Get a reference to PostedFile object Dim attFile As HttpPostedFile = File1.PostedFile ' Get size of the file Dim attachFileLength As Integer = attFile.ContentLength 'Make sure the size of the file is > 0 If attachFileLength > 0 Then ' Get the file name strfilename = Path.GetFileName(File1.PostedFile.FileName) 'Preceed the file name with "attachments/" so 'the file is saved to our Project's attachments Folder strfilename = "Uploads/" + strfilename 'Save the file on the server File1.PostedFile.SaveAs(Server.MapPath(strfilename)) End If End If Dim strConnectionString As String = String.Empty '---------------------------------------------------------- 'strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Driver=Microsoft Text Driver (*.txt;*.csv);Data Source=" + Server.MapPath(strfilename) + ";Extensions=asc,csv,tab,txt;Persist Security Info=False" '---------------------------------------------------------- strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(strfilename) + ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""" Dim cnCSV As New OleDbConnection(strConnectionString) cnCSV.Open() Dim cmdSelect As New OleDbCommand("SELECT * FROM [sheet1$]", cnCSV) cmdSelect.CommandType = CommandType.Text Dim daCSV As New OleDbDataAdapter daCSV.SelectCommand = cmdSelect Dim ds As New DataSet daCSV.Fill(ds) after using the dataset. at last i am trying to delete the file. which give me the
-
Respected Gurus I m using following code to import data from an excel file.. It works fine except one problem that whenever i m trying to delete the file which i have saved on the server by using file.delete(strfilename) i left with the error "you do not have permission" I think i have not closed the excel file. how can i close it programmatically so that i can delete it also. Dim strfilename As String ' Check the first open file dialog for a value If Not (Me.File1.PostedFile Is Nothing) Then ' Get a reference to PostedFile object Dim attFile As HttpPostedFile = File1.PostedFile ' Get size of the file Dim attachFileLength As Integer = attFile.ContentLength 'Make sure the size of the file is > 0 If attachFileLength > 0 Then ' Get the file name strfilename = Path.GetFileName(File1.PostedFile.FileName) 'Preceed the file name with "attachments/" so 'the file is saved to our Project's attachments Folder strfilename = "Uploads/" + strfilename 'Save the file on the server File1.PostedFile.SaveAs(Server.MapPath(strfilename)) End If End If Dim strConnectionString As String = String.Empty '---------------------------------------------------------- 'strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Driver=Microsoft Text Driver (*.txt;*.csv);Data Source=" + Server.MapPath(strfilename) + ";Extensions=asc,csv,tab,txt;Persist Security Info=False" '---------------------------------------------------------- strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(strfilename) + ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""" Dim cnCSV As New OleDbConnection(strConnectionString) cnCSV.Open() Dim cmdSelect As New OleDbCommand("SELECT * FROM [sheet1$]", cnCSV) cmdSelect.CommandType = CommandType.Text Dim daCSV As New OleDbDataAdapter daCSV.SelectCommand = cmdSelect Dim ds As New DataSet daCSV.Fill(ds) after using the dataset. at last i am trying to delete the file. which give me the
-
Respected Gurus I m using following code to import data from an excel file.. It works fine except one problem that whenever i m trying to delete the file which i have saved on the server by using file.delete(strfilename) i left with the error "you do not have permission" I think i have not closed the excel file. how can i close it programmatically so that i can delete it also. Dim strfilename As String ' Check the first open file dialog for a value If Not (Me.File1.PostedFile Is Nothing) Then ' Get a reference to PostedFile object Dim attFile As HttpPostedFile = File1.PostedFile ' Get size of the file Dim attachFileLength As Integer = attFile.ContentLength 'Make sure the size of the file is > 0 If attachFileLength > 0 Then ' Get the file name strfilename = Path.GetFileName(File1.PostedFile.FileName) 'Preceed the file name with "attachments/" so 'the file is saved to our Project's attachments Folder strfilename = "Uploads/" + strfilename 'Save the file on the server File1.PostedFile.SaveAs(Server.MapPath(strfilename)) End If End If Dim strConnectionString As String = String.Empty '---------------------------------------------------------- 'strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Driver=Microsoft Text Driver (*.txt;*.csv);Data Source=" + Server.MapPath(strfilename) + ";Extensions=asc,csv,tab,txt;Persist Security Info=False" '---------------------------------------------------------- strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(strfilename) + ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""" Dim cnCSV As New OleDbConnection(strConnectionString) cnCSV.Open() Dim cmdSelect As New OleDbCommand("SELECT * FROM [sheet1$]", cnCSV) cmdSelect.CommandType = CommandType.Text Dim daCSV As New OleDbDataAdapter daCSV.SelectCommand = cmdSelect Dim ds As New DataSet daCSV.Fill(ds) after using the dataset. at last i am trying to delete the file. which give me the
Your web site runs under an app pool. That app pool has an identity. That user that the app pool is running as needs to be able to delete files in the correct directory. Often it makes sense to create a new app pool for each new web application. Then you can create a network user for that specific application and give that user the correct rights. Ben
-
Your web site runs under an app pool. That app pool has an identity. That user that the app pool is running as needs to be able to delete files in the correct directory. Often it makes sense to create a new app pool for each new web application. Then you can create a network user for that specific application and give that user the correct rights. Ben
Thanks sir resolve it was a permission issue. given the rights and now working smoothly.