文件上传.ashx

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using DSJKYWeb.Common;
using DSJKYWeb.DSJKYWebHandler;
using System.Configuration;

namespace DSJKYWeb.WebHandler
{
    
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class EleUploadHandler : IHttpHandler, IRequiresSessionState
    {
        private HttpContext context = null;
        public void ProcessRequest(HttpContext _context)
        {
            _context.Response.ContentType = "text/plain";
            _context.Response.Charset = "utf-8";
            context = _context;
            HttpPostedFile file = context.Request.Files["Filedata"];
            string id = context.Request["id"];
            #region 上传文本保存的根目录
            string strpath = ConfigurationManager.AppSettings["PassengerDeviceMan"].ToString();
            if (!Directory.Exists(context.Server.MapPath(strpath)))
            {
                Directory.CreateDirectory(context.Server.MapPath(strpath));
            }
            string uploadPath = context.Server.MapPath(strpath);

            #endregion

            var ret = new EleUploadRet();
            string _path = string.Empty;
            if (file == null)
                return;
            _path += strpath + "\\";
             var upname="";
              upname = Guid.NewGuid().ToString() + "." + file.FileName.Split('.')[file.FileName.Split('.').Length - 1];
              _path += upname;
            file.SaveAs(context.Server.MapPath(_path));
            ret.SavePath = _path;
            ret.name = upname;
            context.Response.Write(AjaxResult.Success(ret).ToString());
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }


    /// <summary>
    /// 上传后统一返回类
    /// PS:所有的返回请使用这个类,不要修改原来的已经存在属性,如果你获取新的返回信息,请添加一个新的属性即可
    /// </summary>
    class EleUploadRet
    {
        public string SavePath { get; set; }
        public string name { get; set; }
    }
}

posted @ 2017-04-10 16:44  萌橙  阅读(1051)  评论(0编辑  收藏  举报