Base64文件上传服务端开发源码

using System;
using System.Configuration;
using System.IO;
using System.Security.Cryptography;
using System.Web;

namespace Factory
{
    public class FileBase 
    {
        //文件在服务器存放地址
       public static readonly  string FILE_SRTVICE_PATH = ConfigurationManager.AppSettings["FileService"].ToString();
        public static  ResultDto<dynamic> UpLoad(UpLoadInfo info)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            string filePath = string.Empty;
            string fileName = string.Empty;
            string savePath = HttpContext.Current.Server.MapPath("~/");
            string folderName = string.Empty;
            switch (info.fileType)
            {
                case (1): folderName ="Images/"; break;
                case (2): folderName = "Files/"; break;
                case (3): folderName = "Videos/"; break;
                default: folderName = "Images/"; break;
            }
            savePath += folderName;
            try
            {
                byte[] ibyte = FileConvertUtil.FromBase64ToByte(info.fileByte);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] fileKey = md5.ComputeHash(ibyte);
                string md5Ibyte = Convert.ToBase64String(fileKey);
                md5Ibyte = md5Ibyte.Replace('/', 'f');
                md5Ibyte = md5Ibyte.Replace('@', 'w');
                md5Ibyte = md5Ibyte.Replace('+', 'y');
                md5Ibyte = md5Ibyte.Replace(' ', 'j');
                md5Ibyte = md5Ibyte.Replace('.', 'x');
                md5Ibyte = md5Ibyte.Replace('\\', 'd');
                md5Ibyte = md5Ibyte.Substring(0, 19);
                filePath = md5Ibyte.Substring(0, 4) + "/" +
                           md5Ibyte.Substring(4, 3) + "/" +
                           md5Ibyte.Substring(7, 4);

                fileName = md5Ibyte.Substring(11) + "." + info.extName;
                string fullName = savePath +"/"+filePath+ "/";

                if (!Directory.Exists(fullName))
                {
                    Directory.CreateDirectory(fullName);
                }

                using (FileStream deflultStream = new System.IO.FileStream(fullName+fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
                {
                    deflultStream.Write(ibyte, 0, ibyte.Length);
                    deflultStream.Close();
                }
                result.Status = 1;
                result.Message = "上传成功!!";
                result.Data =  folderName + filePath+"/"+fileName;
            }
            catch(Exception ex)
            {
                result.Status = 0;
                result.Message = "上传异常!!";
            }
            return result;
        }
    }
 }

ResultDto.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    public class ResultDtoBase
    {
        public ResultDtoBase() { }
        /// <summary>
        /// 1.表示成功,其他失败,自定义
        /// </summary>
        public int Status { get; set; }
        public string Message { get; set; }
    }

    public class ResultDto<T> : ResultDtoBase
    {
        public T Data { get; set; }
    }
    public class ResultDto<T,V> : ResultDtoBase
    {
        public T Data { get; set; }
        public V Value { get; set; }
    }
}

 

UpLoadInfo.cs

 

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

namespace Bandi.Kuube.FileService.Models
{
    public class UpLoadInfo
    {
        public string fileByte { get; set; }

        /// <summary>
        ///  后缀名 不需要加点
        /// </summary>
        public string extName { get; set; }

        /// <summary>
        /// 1.图片  2.文件
        /// </summary>
        public int fileType { get; set; }

    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值