• 前端提交后台一般处理文件


    <script src="../JS/jquery.form.min.js"></script>

    添加引用

      $("#form1").ajaxSubmit({
                        success: function (data) {
                           // 成功后要做的事
                        },
                        error: function (error) { alert("上传失败"); },
                        url: '../FileHelper/FileUpLoad.ashx', /*设置post提交到的页面*/
                        data: { id1: request("zjhm"), id2: request("id"), id3: "1" },//参数
                        type: "post", /*设置表单以post方法提交*/
                        dataType: "json" /*设置返回值类型为文本*/
                    });

    前端提交

    HttpPostedFile file = context.Request.Files["imgfile"];
    
                    //Thread.Sleep(3000);
    
    
    
                    //上传文件后缀名检测
                    string filename = file.FileName;
                    string suffix = Path.GetExtension(filename);
                    if (suffix != ".jpg" & suffix != ".jpeg")
                    {
                        context.Response.Write(Maticsoft.COM.Transform.ToJsonString("只允许上传jpg文件"));
                        return;
                    }
                   
    
                   
                    //重命名:DateTime
                    //Random ro = new Random();
                    filename = string.Format("{0}@{1}{2}", context.Request.Form["id1"], context.Request.Form["id2"], suffix);
    
                    if (System.IO.File.Exists(@"../photo/" + filename))
                    {
                        System.IO.File.Delete(@"../photo/" + filename);
                    }
                    //重命名:GUID(全球唯一标识符)推荐!!!
                    //filename = string.Format("{0}{1}", Guid.NewGuid().ToString("N"), suffix);
    
                    //创建目录
                    string dirPath = "";
                    string dirFullPath = context.Server.MapPath("../photo/" + dirPath);
                    string fileFullPath = Path.Combine(dirFullPath, filename);
                    //如果文件夹不存在,则先创建文件夹
                    if (!Directory.Exists(dirFullPath))
                    {
                        Directory.CreateDirectory(dirFullPath);
                    }
    
                    //string filePath = context.Server.MapPath("~/upload") + "/" + filename;
    
                    //保存文件
                    file.SaveAs(fileFullPath);
                    
                    context.Response.Write(Maticsoft.COM.Transform.ToJsonString("ok"));

    这是我上传图片的处理文件

  • 相关阅读:
    POJ 2456 Aggressive cows (二分)
    ACM北大暑期课培训第二天
    ACM北大暑期课培训第一天
    Rust Lang Book Ch.14 Crates.io
    Rust Lang Book Ch.13 Iterators, Closures
    Rust Lang Book Ch.12 An I/O Project: Building a Command Line Program
    Rust Lang Book Ch.11 Automated Tests
    Rust Lang Book Ch.10 Generic Types, Traits. and Lifetimes
    Rust Lang Book Ch.9 Error Handling
    HDU-2087-KMP-水题
  • 原文地址:https://www.cnblogs.com/xiongyang123/p/6698917.html
Copyright © 2020-2023  润新知