如何使用jQuery从服务器文件夹中删除文件 [英] how to dele file from the server Folder using jquery

查看:223
本文介绍了如何使用jQuery从服务器文件夹中删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这里制作mvc3.

我正在尝试从前端将文件上传到文件夹,我不仅要删除,还要从服务器中删除实际文件.即使我需要如何为该文件提供唯一的名称以插入重复的文件name.i需要从前端而不是从数据库plz轻而易举地删除该文件,帮助我完成这项工作

这是控制器中保存上传文件的部分:
================================================== ============

 BugModel bug = ;
            如果(会话[" ] == 为空)
            {
                bug =模型;
            }
            其他
            {

                bug =(BugModel)Session [" ];
            }
             foreach (字符串 inputTagName  in  Request.Files )
            {
                HttpPostedFileBase file1 = Request.Files [inputTagName];
                如果(file1.ContentLength >   0  )
                {
                    字符串 path = "  + Path.GetFileName(file1.FileName);
                    字符串 savedFileName = Path.Combine(Server.MapPath("  +路径));
                    file1.SaveAs(savedFileName);
                    BugAttachment附件=  BugAttachment();
                    attachment.FileName = "  + path.ToString();
                    attachment.AttachmentName = AttachmentName;
                    attachment.AttachmentUrl =附件.文件名;
                    bug.ListFile.Add(attachment);
                    模型=错误;
                    会话[" ] =模型;
                }
            }
            ModelState.Clear();
            返回视图(" ,错误);
        } 


================================================== =======
一旦我将文件保存到服务器中,我就需要从前端应用程序中删除该文件,因此,请事先感谢我

解决方案

jquery可以进行AJAX调用并告诉服务器删除文件.但是,它不能执行文件操作(实际上,它不能执行javascript不能执行的任何操作,因为仅此而已),也不能看到服务器,只能发出HTTP请求.

因此,如已清除,您想在将文件上传到服务器后删除位于客户端的文件.

从您的陈述中可以清楚地看出,该应用程序将在受控环境中运行.因此,您需要一些安全访问权限才能从文件夹中删除文件.

在Javascrip中,尝试此操作..

<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>



希望对您有所帮助.
欢呼


  public  ActionResult Delete( string 文件名)
      {

         char  DirSeparator = System.IO.Path.DirectorySeparatorChar;

        字符串 FilesPath = "  + FileName ;
        字符串 filenameonly =名称+ Path.GetFileName(FilesPath);
        字符串 FPath = "  + DirSeparator + "  + DirSeparator +仅文件名;
        // 如果没有名称,则不做任何事情
        如果(FileName.Length ==  0 )>返回 View();
        // 设置删除的完整路径
        字符串 path = FilesPath + DirSeparator;
        // 检查我们的文件是否存在
        如果(System.IO.File.Exists(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + FPath)))
        {
            // 删除我们的文件
            System.IO.File.Delete(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + FPath));

        }
         模型=错误;
        会话[" ] =模型;

       
        返回视图(" ,错误);
    }
    公共 字符串 name { get ; 设置; }
    公共 对象 Bug { get ; 设置; }
} 


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:
===============================================================

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

解决方案

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.


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


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; }
}


这篇关于如何使用jQuery从服务器文件夹中删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆