layui-多个文件同时上传

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadFiles.aspx.cs" Inherits="TCIQDOA.FilesManager.View.UploadFiles" %>




	
	上传文件
	
	
	
	
	
	


	

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace TCIQDOA.FilesManager.Controller
{
    /// 
    /// UploadFiles 的摘要说明
    /// 
    public class UploadFiles : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string cmd = context.Request["cmd"];
            switch (cmd)
            {
                case "UploadSomeFiles":
                    {
                        UploadSomeFiles(context);
                        break;
                    }
            }
        } 
        private void UploadSomeFiles(HttpContext context)
        {
            string path = "";
            string name = "";
            try
            {
                int count = context.Request.Files.Count;
                for (int i = 0; i < count; i++)
                {
                    int contentLength = context.Request.Files[i].ContentLength;//文件的大小
                    string contentType = context.Request.Files[i].ContentType;//文件的类型
                    string localPath= context.Request.Files[i].FileName;//文件的本地路径
                    string extension = Path.GetExtension(localPath).ToLower();//文件的后缀
                    string oldName = Path.GetFileName(localPath);
                    string newName = DateTime.Now.ToString("yyyyMMddHHmm_") + oldName.Replace(" ", "_").Replace("&", "_");
                    string filePath = GetPathForSaveFolder(); 
                    string fileSavePath = HttpContext.Current.Server.MapPath(filePath);
                    fileSavePath = fileSavePath + newName;  
                    context.Request.Files[i].SaveAs(fileSavePath);
                    path = filePath + newName;
                    name = oldName;
                }   
            }
            catch (Exception ex)
            {
                context.Response.Write("{\"isSuccess\":\"false\",\"url\":\"上传失败\"}"); 
            }
            finally
            {
                context.Response.Write("{\"isSuccess\":\"true\",\"url\":\"" + path + "\",\"name\":\"" + name + "\"}");
            }  
        } 
        private string GetPathForSaveFolder()
        {
            string path = GetAppPath() + "UploadFile/items/" + DateTime.Now.ToString("yyyy-MM") + "/";
            string fileSavePath = HttpContext.Current.Server.MapPath(path);
            if (!Directory.Exists(fileSavePath))
            {
                Directory.CreateDirectory(fileSavePath);
            } 
            return path;
        }
        private string GetAppPath()
        {
            string applicationPath = HttpContext.Current.Request.ApplicationPath;
            if (!applicationPath.EndsWith("/"))
            {
                applicationPath = applicationPath + "/";
            }
            return applicationPath;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

你可能感兴趣的:(H5,css,js,ASP.Net)