Click here to Skip to main content
15,919,567 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i am working on mvc3 here.

I am trying to upload files to a folder from The front-end , I would like to not only delete but also remove the actual file from the server.even i need to how should i provide a unique name for that file to insert duplicate file name.i need to delete that file from frontend tempererly not from database plz help me to do this work

Here is the part of my controller that saves the uploaded file:
===============================================================
C#
BugModel bug = null;
            if (Session["CaptureData"] == null)
            {
                bug = model;
            }
            else
            {

                bug = (BugModel)Session["CaptureData"];
            }
            foreach (string inputTagName in Request.Files)
            {
                HttpPostedFileBase file1 = Request.Files[inputTagName];
                if (file1.ContentLength > 0)
                {
                    string path = "/Content/UploadedFiles/" + Path.GetFileName(file1.FileName);
                    string savedFileName = Path.Combine(Server.MapPath("~" + path));
                    file1.SaveAs(savedFileName);
                    BugAttachment attachment = new BugAttachment();
                    attachment.FileName = "~" + path.ToString();
                    attachment.AttachmentName = AttachmentName;
                    attachment.AttachmentUrl = attachment.FileName;
                    bug.ListFile.Add(attachment);
                    model = bug;
                    Session["CaptureData"] = model;
                }
            }
            ModelState.Clear();
            return View("LoadBug", bug);
        }

=========================================================
once i saved that file into server i need to delete that file from front end application so could u plz help me to how to do this work thanks in advance
Posted
Updated 23-Aug-12 2:30am
v2
Comments
ZurdoDev 23-Aug-12 8:31am    
What do you mean by deleting from the front end application?
shakil4u 23-Aug-12 8:46am    
means i am storing the files in separate folder with the name UploadedFiles i need to delete from it
ZurdoDev 23-Aug-12 9:28am    
Just use the File object. http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx
Sandip.Nascar 23-Aug-12 9:04am    
You have confused with frontend and server. What I have understood, you are uploading file from client end to server and once the file is uploaded you need to delete the local files. Is it what you intend to do?
shakil4u 23-Aug-12 10:27am    
yes

jquery can make an AJAX call and tell the server to delete a file. But, it cannot do file operations ( actually, it can't do anything javascript can't do, as that's all it is ), nor can it see the server, except to make HTTP requests.
 
Share this answer
 
So, as you have cleared, you want to delete file(s) located in client side after you upload the file(s) to the server.

As it is clear from your statement that this application will be run from a controlled environment. So, you need some security access permission to delete the files from the folder.

In Javascrip, try this..
<script type="text/javascript">
    // initialize ActiveXObject and create an object of Scripting.FileSystemObject.
    var fso = new ActiveXObject('Scripting.FileSystemObject');
    fso.DeleteFile("C:\\Temp\\customefolder\\file.txt, true);

    fso = null;
</script>


hope this helps.
cheers
 
Share this answer
 
C#
public ActionResult Delete(string FileName)
      {

        char DirSeparator = System.IO.Path.DirectorySeparatorChar;

        string FilesPath = ";" + FileName;
        string filenameonly = name + Path.GetFileName(FilesPath);
        string FPath = "Content" + DirSeparator + "UploadedFiles" + DirSeparator + filenameonly;
        // Don't do anything if there is no name
        if (FileName.Length == 0) return View();
        // Set our full path for deleting
        string path = FilesPath + DirSeparator;
        // Check if our file exists
        if (System.IO.File.Exists(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + FPath)))
        {
            // Delete our file
            System.IO.File.Delete(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + FPath));

        }
         model = bug;
        Session["CaptureData"] = model;

       
        return View("LoadBug", Bug);
    }
    public string name { get; set; }
    public object Bug { get; set; }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900