zoukankan      html  css  js  c++  java
  • 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());
                    }
                }
            }
    
      
  • 相关阅读:
    菜根谭#250
    菜根谭#249
    菜根谭#248
    [转载]将网卡(设备中断)绑定到特定CPU
    request_mem_region,ioremap 和phys_to_virt()
    [转载]python的range()函数用法
    [转载]Python print函数用法,print 格式化输出
    [转载]python datetime处理时间
    Linux驱动
    Linux驱动
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/11132315.html
Copyright © 2011-2022 走看看