• .net文件上传,客户端用jquery file upload


    <%@ WebHandler Language="C#" Class="Handler" %>
    
    using System;
    using System.Web;
    
    
    using System.IO;
    
    
    public class Handler : IHttpHandler {
    
        /// <summary>
        /// 上传文件夹
        /// </summary>
        private const string UPLOAD_FOLDER = "~/UploadFile/";
    
        public void ProcessRequest(HttpContext context)
        {
            int resultVal = (int)ReturnVal.Failed;
            try
            {
                HttpPostedFile myFile = context.Request.Files[0];
    
                if (myFile != null)
                {
                    if (myFile.InputStream.Length != 0)
                    {
                        string originalFileName = Path.GetFileName(myFile.FileName);     //原文件名                        
                        string newFileName = string.Format("{0}_{1}", Guid.NewGuid(), originalFileName);   //新文件名---组成形式:  GUID + 下划线 + 原文件名
                        string fileAbsPath = context.Server.MapPath(UPLOAD_FOLDER) + newFileName;   //绝对路径
    
                        myFile.SaveAs(fileAbsPath);
    
                        resultVal = (int)ReturnVal.Succeed;
                    }
                    else
                    {
                        resultVal = (int)ReturnVal.FileEmpty;
                    }
                }
                else
                {
                    resultVal = (int)ReturnVal.NotSelected;
                }
            }
            catch (Exception)
            {
                resultVal = (int)ReturnVal.Failed;
            }
            finally
            {
                context.Response.Write(resultVal);
            }
        }
    
        #region## 返回值
        /// <summary>
        /// 返回值
        /// </summary>
        private enum ReturnVal : int
        {
            /// <summary>
            /// 不能上传 0 K大小的文件
            /// </summary>
            FileEmpty = -2,
    
            /// <summary>
            /// 未选中文件
            /// </summary>
            NotSelected = -1,
    
            /// <summary>
            /// 上传失败
            /// </summary>
            Failed = 0,
    
            /// <summary>
            /// 成功
            /// </summary>
            Succeed = 1
    
        }
        #endregion
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    
    }

  • 相关阅读:
    CF821E 【Okabe and El Psy Kongroo】
    BZOJ1231: [Usaco2008 Nov]mixup2 混乱的奶牛
    P1896 [SCOI2005]互不侵犯
    QBXT Day 2 记录
    CF467C George and Job
    【luogu P3373 线段树2】 模板
    【luogu P1306 斐波那契公约数】 题解
    【luogu T24743 [愚人节题目5]永世隔绝的理想乡】 题解
    【luogu P1903 [国家集训队]数颜色】 题解
    莫队算法~讲解
  • 原文地址:https://www.cnblogs.com/tongdengquan/p/6090518.html
Copyright © 2020-2023  润新知