H5文件上传

htmL代码:
html
      

 
     
   
 
   

   

   

     
   

   

               
               

               
               
/html
JS代码:
js
     
/js

后台ashx代码:
c#
    context.Response.ContentType = "text/plain";
            HttpPostedFile  file =context. Request.Files"fileToUpload";
            String fileName = file.FileName;
            String fileExt = Path.GetExtension(fileName).ToLower();

            //定义允许上传的文件扩展名
            Hashtable extTable = new Hashtable();
            extTable.Add("image", "gif,jpg,jpeg,png,bmp");
            extTable.Add("flash", "swf,flv");
            extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
            extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

            if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable"image").Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                context.Response.Write(LitJson.JsonMapper.ToJson(new { status="no", info = "上传文件扩展名是不允许的扩展名。 只允许" + ((String)extTable"image") + "格式。" }));
                     
                return;
            }
            string dirpath = "/upload/"+DateTime.Now.ToString("yyyyMMdd")+"/";
            if (!Directory.Exists(context.Server.MapPath( dirpath)))
            {
                Directory.CreateDirectory(context.Server.MapPath(dirpath));
            }
            String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;

            file.SaveAs(context.Server.MapPath(dirpath) + newFileName);
            context.Response.Write(LitJson.JsonMapper.ToJson(new { status="ok",path = dirpath+newFileName}));
 

你可能感兴趣的:(html/css)