Dixit Gokhale
Dixit Gokhale

Reputation: 671

How to delete a file from App_Data folder in asp.net mvc 3 application?

I read an excel file from the user using

 <input type="file" name="file" />

send it to the controller and store it in App_Data folder. I read that file. After I am done I wish to delete the file from App_Data folder. How do I do that? This is how I store it.

    public ActionResult importFile1(HttpPostedFileBase file)
    {

        if (file != null && file.ContentLength > 0)
        {
            // extract only the fielname
            var fileName = Path.GetFileName(file.FileName);
            // store the file inside ~/App_Data/uploads folder
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
        }
    }

Upvotes: 2

Views: 8321

Answers (2)

Arpita
Arpita

Reputation: 71

Another way for deleting file is:

FileInfo file = new FileInfo("~/App_Data/uploads/image.jpg");
file.Delete();

Thank you.

Upvotes: 1

Mark van Straten
Mark van Straten

Reputation: 9425

Have you tried using

File.Delete("~/App_Data/uploads/myfile.xls");

After processing the file has finished?

Upvotes: 4

Related Questions