上传单个文件的代码

        /// <summary>
        /// 单文件上传
        /// </summary>
        /// <param name="extList">文件格式:如:“.xls”</param>
        /// <param name="SavePath">文件保存路径,如:“/Content/File/Excel/Uploading/”</param>
        /// <param name="refFileName">接收服务器文件所保存的文件名</param>
        /// <returns>返回值小于0为错误,请调用uploadingErrorInfo返回具体错误信息</returns>

        public int uploading_return_FilePath(List<string> extList, string SavePath, ref string refFileName)
        {
            //--上传文件方法体开始
            if (HttpContext.Current.Request.Files.Count == 0)
            {
                refFileName = "-1";
                return -1;
            }
            var c = HttpContext.Current.Request.Files[0];
           
            if (c != null && c.ContentLength > 0)
            {
                string ext = Path.GetExtension(c.FileName);//获得文件扩展名
                if (extList.Contains(ext))
                {


                    int lastSlashIndex = c.FileName.LastIndexOf("\\");
                    string fileName = c.FileName.Substring(lastSlashIndex + 1, c.FileName.Length - lastSlashIndex - 1 - 4);
                    string fileNames = Guid.NewGuid().ToString();
                    string nowdatetime = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();


                    #region old
                    fileName = Path.Combine(HttpContext.Current.Server.MapPath("~" + SavePath), fileNames + nowdatetime + ext);
                    c.SaveAs(fileName);
                    #endregion


                    refFileName = fileNames + nowdatetime + ext;


                    return 1;
                }
                else
                {
                    refFileName = "-2";
                    return -2;
                }
            }
            else
            {
                refFileName = "-3";
                return -3;
            }

        }


        /// <summary>
        /// 返回错误信息
        /// </summary>
        /// <param name="val">上传方法返回的错误值</param>
        /// <returns></returns>

        public string uploadingErrorInfo(int val)
        {
            switch (val)
            {
                case -1:
                    return "该页面没有上传控件!";
                case -2:
                    return "文件格式出错!";
                case -3:
                    return "请选择需要上传的文件!";
            }
            return "error";
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值