文件上传.ashx

本文介绍了一个基于ASP.NET的Web服务文件上传处理程序的实现方式。该示例展示了如何通过HTTP请求接收上传文件,并将其保存到指定路径。此外还提供了返回上传状态及文件名的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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; }
    }
}

转载于:https://www.cnblogs.com/daimaxuejia/p/6689723.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值