ASP.NET 一般处理程序 接收文件上传

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            StringBuilder sb = new StringBuilder();
            string resultcode = null;
            string errormsg = null;
            string paths = "";
            string names = "";
            string title = "";
            string FILEGUID = "";
            if (context.Request.Files.Count == 0)
            {
                resultcode = "1003";
                errormsg = "上传文件为空!";
                sb.Append("{\"resultcode\":" + "\"" + resultcode + "\"");
                sb.Append(",");
                sb.Append("\"errormsg\":" + "\"" + errormsg + "\"");
                sb.Append(",");
                sb.Append("\"datas\":" + "{");
                sb.Append("\"FilePath\":" + "\"" + paths + "\"");
                sb.Append(",");
                sb.Append("\"FileName\":" + "\"" + names + "\"");
                sb.Append(",");
                sb.Append("\"FileTitle\":" + "\"" + title + "\"");
                sb.Append(",");
                sb.Append("\"FileGuid\":" + "\"" + FILEGUID + "\"");
                sb.Append("}");
                sb.Append("}");
            }
            else
            {
                string guid = System.Guid.NewGuid().ToString("N");

                string dir = context.Server.MapPath("~/TaskMobile/files/") + guid;
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }

                for (int i = 0; i < context.Request.Files.Count; i++)
                {
                    //获取上传的文件的对象  
                    HttpPostedFile img = context.Request.Files[i];

                    //获取上传文件的名称  
                    string s = img.FileName;
                    var strFileExtension = s.Substring(s.LastIndexOf('.') + 1, s.Length - s.LastIndexOf('.') - 1).ToUpper();
                    String allowextension = System.Configuration.ConfigurationManager.AppSettings["AllowUploadFileType"];
                    if (allowextension.IndexOf(strFileExtension) >= 0)
                    {
                        int len = s.LastIndexOf(".") - s.LastIndexOf("\\") - 1;
                        string fileName = s.Substring(s.LastIndexOf("\\") + 1, len);
                        string fullName = s.Substring(s.LastIndexOf("\\") + 1);

                        fileName = fileName.Replace(",", "");
                        fullName = fullName.Replace(",", "");
                        fullName = fullName.Replace(":", "");
                        //截取获得上传文件的名称(ie上传会把绝对路径也连带上,这里只得到文件的名称)  
                        //string str = System.Guid.NewGuid().ToString("N")+ s.Substring(s.LastIndexOf(".") - 1); // s.Substring(s.LastIndexOf("\\") + 1);
                        string path = dir + "\\" + fullName;
                        //保存文件  
                        img.SaveAs(path);
                        paths = HttpRuntime.AppDomainAppVirtualPath + "/TaskMobile/files/" + guid + "/" + fullName;
                        title = "/TaskMobile/files/" + guid + "/" + fullName;
                        names = fullName;

                        //附件大写(单位KB)
                        int size = img.ContentLength / 1024;
                        //保存附件到文件表
                        FILEGUID = new NDFILES().AddFiles(names, "", strFileExtension, NDFILES.getFileType(strFileExtension), size.ToString(), paths, context.User.Identity.Name);

                        resultcode = "1000";
                        sb.Append("{\"resultcode\":" + "\"" + resultcode + "\"");
                        sb.Append(",");
                        sb.Append("\"errormsg\":" + "\"" + errormsg + "\"");
                        sb.Append(",");
                        sb.Append("\"datas\":" + "{");
                        sb.Append("\"FilePath\":" + "\"" + paths + "\"");
                        sb.Append(",");
                        sb.Append("\"FileName\":" + "\"" + names + "\"");
                        sb.Append(",");
                        sb.Append("\"FileTitle\":" + "\"" + title + "\"");
                        sb.Append(",");
                        sb.Append("\"FileGuid\":" + "\"" + FILEGUID + "\"");
                        sb.Append("}");
                        sb.Append("}");
                    }
                    else
                    {
                        resultcode = "1003";
                        errormsg = "不支持的文件类型:" + strFileExtension;
                        sb.Append("{\"resultcode\":" + "\"" + resultcode + "\"");
                        sb.Append(",");
                        sb.Append("\"errormsg\":" + "\"" + errormsg + "\"");
                        sb.Append(",");
                        sb.Append("\"datas\":" + "{");
                        sb.Append("\"FilePath\":" + "\"" + paths + "\"");
                        sb.Append(",");
                        sb.Append("\"FileName\":" + "\"" + names + "\"");
                        sb.Append(",");
                        sb.Append("\"FileTitle\":" + "\"" + title + "\"");
                        sb.Append(",");
                        sb.Append("\"FileGuid\":" + "\"" + FILEGUID + "\"");
                        sb.Append("}");
                        sb.Append("}");
                    }
                    context.Response.Write(sb.ToString());
                }
            }
        }

  

转载于:https://www.cnblogs.com/qiang277629628/p/9254443.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值