2

I am creating an Excel file on a web server, using OleDb to connect the the physical (well as physical as it can be) file and appending records. I am then returning a FilePathResult to the user via MVC, and would like to delete the physical file afterwards due to data protection concerns over the appended records.

I have tried using a File.Delete in a Finally clause but I get a File Not Found error which must mean the file has gone when MVC is trying to send the file to the user.

I thought about creating the File as a MemoryStream but I think OleDb needs a physical file to connect to so this isn't an option.

Any suggestions on how to delete the file after returning it in one operation?

Edit

As requested this is what I'm working from, though I'm not sure how it helps :)

    Public Function ExportAllOutputs() As FilePathResult

        ' Create Export File Name
        Dim ExportFilename As String = Replace(Me.Name, " ", "_") & "_Outputs.xls"

        Try

            ' Create Export File
            CreateExportFile(ExportFilename)

            ' Populate Export File
            For Each OutputType As OutputTypeEnum In [Enum].GetValues(GetType(OutputTypeEnum))
                ExportHelper.AppendOutputs(ExportFilepath & ExportFilename, Me.GetOutputs(OutputType), Me.ProgrammeID)
            Next

            ' Return Export File
            Return ReturnExportFile(ExportFilename)

        Catch ex As Exception
            Throw
        Finally
            'If IO.File.Exists(ExportFilepath & ExportFilename) Then IO.File.Delete(ExportFilepath & ExportFilename)
        End Try

    End Function
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
David A Gibson
  • 2,013
  • 5
  • 35
  • 60
  • You should be aware that OleDb is not supported on 64 bit machines, so you should find another solution for future proofing your code. – Cine May 28 '10 at 09:35
  • Could you post some code for this? – David Neale May 28 '10 at 09:36
  • I was NOT aware that OleDb is not 64bit - I will have to make a note of that but it isn't an issue at the moment, cheers Cine, also I have added the function where I think a solution would go but not sure it really helps. – David A Gibson May 28 '10 at 10:16
  • Also I think I may end up doing as suggested here http://stackoverflow.com/questions/2709958/c-on-quit-webpage-delete-files-and-folders-on-server-with-no-user-action if I don't get any answers. – David A Gibson May 28 '10 at 10:17

1 Answers1

1

Somewhat hacky, but you could do as below:

  1. read the file into a byte array in memory using File.ReadAllBytes(),
  2. delete the file,
  3. form a memory stream around the byte array
  4. get MVC to return data via the stream using FileStreamResult.

Clearly, if the files are large then you may get memory pressures.

Neil Moss
  • 6,598
  • 2
  • 26
  • 42
  • Hi Neil, that makes perfect sense - create the real file using ADO - THEN read it into a Memory Stream - then delete it - so simple :) Cheers for that – David A Gibson Jun 01 '10 at 07:35