zoukankan      html  css  js  c++  java
  • 删除服务端文件

    今天在winform上需要上传,删除服务器上的文件,网上找了下上传是很多,可删除没有找到答案,,最后只能拿个土办法实现了,这里是用ashx文件来处理
    上传:
    B/S
     
     public void ProcessRequest(HttpContext context)
            {
                foreach (string fileKey in context.Request.Files)
                {
                    try
                    {
                        HttpPostedFile file = context.Request.Files[fileKey];
                        string oldFileName = file.FileName; //原文件名  xxx.jpg
                        int extenIndex = oldFileName.LastIndexOf(".");
                        string getExtent = oldFileName.Substring(extenIndex);//文件扩展名  .jpg
                        string fileSize = UploadCommon.getFileSize(file.ContentLength);  //获取文件大小 
                        string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssms") + (new Random()).Next(0, 9) + getExtent;
                        string path = "/AffixFiles/sw_Products/";
                        if (!Directory.Exists(context.Server.MapPath(path)))
                        {
                            Directory.CreateDirectory(context.Server.MapPath(path));
                        }
                        string filePath = Path.Combine(path, newFileName);//文件相对路径
                        file.SaveAs(context.Server.MapPath(filePath));
                        string json = "{"status":"success","oldFileName":"" + oldFileName + "","fileSize":"" + fileSize + "","filePath":"" + filePath + "","fileType":"" + getExtent + ""}";
                        context.Response.Write(json);
                    }
                    catch
                    {
                        string json = "{"status":"error","oldFileName":"" + null + "","fileSize":"" + null + "","filePath":"" + null + "","fileType":"" + null + ""}";
                        context.Response.Write(json);
                    }
                    context.Response.End();
                }
            }
    C/S
     
    private void 上传_Click(object sender, EventArgs e)
            {
                System.Net.WebClient myWebClient = new System.Net.WebClient();
                byte[] b = myWebClient.UploadFile("http://190.160.10.13:8088/ClientUpload/ClientUploadTools.ashx",
                    "POST", "C:\1351040562_Download.png");
            
               string returnJson = Encoding.Default.GetString(b);
               JavaScriptSerializer json = new JavaScriptSerializer();
               uploadMessage result = json.Deserialize<uploadMessage>(returnJson);//最后的返回结果集
               
            }
            public class uploadMessage
            {
                public string oldFileName { get; set; }
                public string fileSize { get; set; }
                public string filePath { get; set; }
                public string fileType { get; set; }
                public string status { get; set; }
            }
     
     
    删除:
    这里删除就使用了 UploadData方法来处理,太土了,大家有没有更好的办法提供一下哈
    B/S
     
     public void ProcessRequest(HttpContext context)
            {
                try
                {
                    int totalBytes = context.Request.TotalBytes;
                    if (totalBytes > 0)
                    {
                        byte[] readBytes = context.Request.BinaryRead(totalBytes);
                        string filePath = System.Text.Encoding.Default.GetString(readBytes);
                        string overFile = context.Server.MapPath(filePath);
                        if (File.Exists(overFile))
                        {
                            try
                            {
                                File.Delete(overFile);
                                context.Response.Write("success");
                            }
                            catch
                            {
                                context.Response.Write("error");
                            }
                        }
                    }
                }
                catch
                {
                    context.Response.Write("error");
                }
                context.Response.End();
            }
     
    C/S
     
    /// <summary>
            /// 删除成功success  删除失败 error
            /// </summary>
            private void 删除_Click(object sender, EventArgs e)
            {
                System.Net.WebClient myWebClient = new System.Net.WebClient();
                byte[] b = myWebClient.UploadData("http://localhost:2666/ClientUpload/ClientDeleteTool.ashx",
                    "POST", Encoding.Default.GetBytes("/AffixFiles/sw_Products/2012112211243824388.png"));
                string result = Encoding.Default.GetString(b);//返回的结果
            }
     
    好文要顶 关注我 收藏该文 
  • 相关阅读:
    springcloud-Ribbon之手写轮询算法
    springcloud-Ribbon负载均衡规则的替换
    git本地库和远程库的连接和断开
    本地项目第一次提交到码云或github
    python基础语法练习
    Xss-labs-level11-15
    Vulnhub-靶机-ESCALATE_LINUX: 1
    Xss-labs-level7-10
    Vulnhub-靶机-DC: 6
    Jenkins入门之执行Powershell脚本
  • 原文地址:https://www.cnblogs.com/qiu18359243869/p/10989674.html
Copyright © 2011-2022 走看看