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

Posted on 2017-04-12 14:07  熊先生丶  阅读(281)  评论(0编辑  收藏  举报
<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"));

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