Exemple #1
0
 /// <summary>
 /// 构造函数,指定存储文件夹
 /// </summary>
 /// <param name="OldName">旧文件名</param>
 /// <param name="ContentLength">文件大小</param>
 /// <param name="UploadPath">上传文件夹</param>
 public UploadInfo(string OldName, int ContentLength, string UploadPath)
 {
     this.OldName       = OldName;
     this.ContentLength = ContentLength;
     this.NewName       = UploadInfo.CreateNewName(Extention);
     this.UploadPath    = UploadPath;
 }
Exemple #2
0
        /// <summary>
        /// 接收文件,逻辑处理
        /// </summary>
        private void DoWork()
        {
            HttpRequest req = _Context.Request;
            //判断是否 有fileinfo,则代表创建文件
            string info = req["fileinfo"] as string;

            if (string.IsNullOrEmpty(info) == false)
            {
                //创建文件
                try
                {
                    UploadMsg upMsg = info.JsonDese <UploadMsg>();
                    if (upMsg == null)
                    {
                        throw new HttpException(500, "服务器接收文件信息json数据失败");
                    }
                    if (string.IsNullOrEmpty(this.SubFolder) == false)
                    {
                        upMsg.SubFolder = this.SubFolder;
                    }
                    this.file = new UploadInfo(upMsg);
                    //接收文件信息成功
                    SendSuccess("接收文件信息,并创建问价成功");
                }
                catch (Exception ex)
                {
                    SendError(ex);
                }
            }
            else
            {
                //接收文件
                if (req.Files.Count <= 0)
                {
                    throw new HttpException(500, "获取上传文件失败");
                }
                HttpPostedFile _file    = req.Files[0];
                string         backInfo = req["backinfo"] as string;
                if (string.IsNullOrEmpty(backInfo))
                {
                    throw new HttpException("获取文件信息失败");
                }

                UploadMsg upMsg = backInfo.JsonDese <UploadMsg>();
                this.file = new UploadInfo(upMsg);
                //获取文件数据
                Stream stream = _file.InputStream;
                try
                {
                    byte[] dataOne = new byte[stream.Length];
                    stream.Read(dataOne, 0, dataOne.Length);
                    AppendFile(dataOne);
                }
                finally
                {
                    stream.Close();
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="file"></param>
 public SuccessInfo(UploadInfo file)
 {
     this.newName = file.NewName;
     this.relativeName = file.GetRelativeName();
     this.size = file.ContentLength;
     this.Data = file.Data;
     this.handleType = file._UploadMsg.HandleType;
 }
Exemple #4
0
 /// <summary>
 /// 详细构造信息
 /// </summary>
 /// <param name="file">文件信息</param>
 /// <param name="ex">错误信息</param>
 public ErrorInfo(UploadInfo file, Exception ex) : this(ex)
 {
     if (file != null)
     {
         this.newName      = file.NewName;
         this.relativeName = file.GetRelativeName();
     }
 }
Exemple #5
0
        /// <summary>
        /// 接收文件,逻辑处理
        /// </summary>
        public async Task DoWork()
        {
            HttpRequest req = _Context.Request;

            //创建文件
            try
            {
                //接收文件
                if (req.Form.Files.Count <= 0)
                {
                    throw new Exception("获取上传文件失败");
                }
                IFormFile _file    = req.Form.Files[0];
                string    backInfo = req.Form["backinfo"];
                if (string.IsNullOrEmpty(backInfo))
                {
                    throw new Exception("获取文件信息失败");
                }
                UploadMsg upMsg = backInfo.JsonDeserialize <UploadMsg>();
                if (upMsg == null)
                {
                    throw new Exception("服务器接收文件信息json数据失败");
                }
                if (string.IsNullOrEmpty(this.SubFolder) == false)
                {
                    upMsg.SubFolder = this.SubFolder;
                }
                if (string.IsNullOrEmpty(upMsg.OldName))
                {
                    upMsg.OldName = _file.FileName;
                }

                this.file = new UploadInfo(upMsg);
                using (FileStream fs = new FileStream(this.file.GetFullName(), FileMode.OpenOrCreate, FileAccess.Write))
                {
                    Stream reader = _file.OpenReadStream();
                    byte[] list   = new byte[reader.Length];
                    await reader.ReadAsync(list, 0, list.Length);

                    reader.Close();

                    await fs.WriteAsync(list, 0, list.Length);

                    fs.Close();
                    fs.Dispose();
                }
                //接收文件信息成功
                SendSuccess("接收文件信息,并创建问价成功");
            }
            catch (Exception ex)
            {
                SendError(ex);
            }
        }
Exemple #6
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="info">上传文件信息</param>
 public ThumbnailHandle(UploadInfo info, string folder, int width)
 {
     this.Folder   = folder;
     this._Info    = info;
     this.FullName = info.GetFullName();
     if (string.IsNullOrEmpty(info.GetSubFolder()))
     {
         throw new Exception("缩略图处理模式,上传子目录不能为空");
     }
     this.Width = width;
 }
Exemple #7
0
 /// <summary>
 /// 构造函数
 /// 需要加载WebConfig中的上传节点
 /// </summary>
 /// <param name="OldName">旧文件名</param>
 /// <param name="ContentLength">文件大小</param>
 public UploadInfo(string OldName, int ContentLength, bool IsRename)
 {
     this.OldName       = OldName;
     this.ContentLength = ContentLength;
     //如果是第一次创建链接需要创建新名称
     if (IsRename)
     {
         this.NewName = UploadInfo.CreateNewName(Extention);
     }
     else
     {
         this.NewName = OldName;
     }
     //加载配置文件中上传文件夹
     this.UploadPath = GetUploadFiles();
 }
Exemple #8
0
 /// <summary>
 /// 根据客户端json信息,创建
 /// </summary>
 /// <param name="upMsg"></param>
 public UploadInfo(UploadMsg upMsg, string AbolutePath = null)
 {
     this.OldName       = upMsg.OldName;
     this.ContentLength = upMsg.Size;
     if (string.IsNullOrEmpty(upMsg.NewName))
     {
         this.NewName = UploadInfo.CreateNewName(Extention);
     }
     else
     {
         this.NewName = upMsg.NewName;
     }
     _UploadMsg = upMsg;
     if (upMsg.HandleType == HandleType.自动处理.GetHashCode())
     {
         //自动模式,上传到网站upload文件夹
         this.UploadPath = new AutoHandle(upMsg.SubFolder).GetAbsolutePath();
     }
     else if (upMsg.HandleType == HandleType.简单处理.GetHashCode())
     {
         //简单处理模式,直接保存到指定uploadpath中
         if (string.IsNullOrEmpty(AbolutePath))
         {
             //获取配置文件中的目录
             string configPath = GetUploadFiles();
             if (string.IsNullOrEmpty(upMsg.SubFolder) == false)
             {
                 configPath += upMsg.SubFolder;
             }
             if (Directory.Exists(configPath) == false)
             {
                 Directory.CreateDirectory(configPath);
             }
             this.UploadPath = configPath;
         }
         else
         {
             this.UploadPath = AbolutePath;
         }
     }
     else  //如果是带临时处理的,保存到临时文件夹
     {
         //加载配置文件中上传文件夹
         this.UploadPath = GetTempFile();
     }
 }
Exemple #9
0
        /// <summary>
        /// 接收文件,逻辑处理
        /// </summary>
        private void DoWork()
        {
            HttpRequest req = _Context.Request;

            //创建文件
            try
            {
                //接收文件
                if (req.Files.Count <= 0)
                {
                    throw new HttpException(500, "获取上传文件失败");
                }
                HttpPostedFile _file    = req.Files[0];
                string         backInfo = req["backinfo"] as string;
                if (string.IsNullOrEmpty(backInfo))
                {
                    throw new HttpException("获取文件信息失败");
                }
                UploadMsg upMsg = backInfo.JsonDese <UploadMsg>();
                if (upMsg == null)
                {
                    throw new HttpException(500, "服务器接收文件信息json数据失败");
                }
                if (string.IsNullOrEmpty(this.SubFolder) == false)
                {
                    upMsg.SubFolder = this.SubFolder;
                }
                if (string.IsNullOrEmpty(upMsg.OldName))
                {
                    upMsg.OldName = _file.FileName;
                }

                this.file = new UploadInfo(upMsg);
                _file.SaveAs(this.file.GetFullName());//保存图片处理

                //接收文件信息成功
                SendSuccess("接收文件信息,并创建问价成功");
            }
            catch (Exception ex)
            {
                SendError(ex);
            }
        }
Exemple #10
0
        /// <summary>
        /// 根据客户端json信息,创建
        /// </summary>
        /// <param name="upMsg"></param>
        public UploadInfo(UploadMsg upMsg, string AbolutePath = null)
        {
            this.OldName       = upMsg.OldName;
            this.ContentLength = upMsg.Size;

            if (string.IsNullOrEmpty(upMsg.NewName))
            {
                this.NewName = UploadInfo.CreateNewName(Extention);
            }
            else
            {
                this.NewName = upMsg.NewName;
            }

            _UploadMsg = upMsg;
            if (upMsg.HandleType == HandleType.自动处理.GetHashCode())
            {
                //自动模式,上传到网站upload文件夹
                this.UploadPath = new AutoHandle(upMsg.SubFolder).GetAbsolutePath();
            }
            else if (upMsg.HandleType == HandleType.简单处理.GetHashCode())
            {
                //简单处理模式,直接保存到指定uploadpath中
                if (string.IsNullOrEmpty(AbolutePath))
                {
                    //获取配置文件中的目录
                    this.UploadPath = ServerInfo.GetSub(upMsg.SubFolder);;
                }
                else
                {
                    this.UploadPath = AbolutePath;
                }
            }
            else  //如果是带临时处理的,保存到临时文件夹
            {
                //加载配置文件中上传文件夹
                this.UploadPath = GetTempFile();
            }
        }
Exemple #11
0
        /// <summary>
        /// socket监听方法
        /// </summary>
        /// <param name="arg">当前WebSocket上下文</param>
        /// <returns></returns>
        public async Task DoWork()
        {
            WebSocket _socket = await _Context.WebSockets.AcceptWebSocketAsync();

            this._socket = _socket;
            //监视相应
            while (true)
            {
                /*
                 * 接收客户端数据
                 */
                ArraySegment <byte>    buffer = new ArraySegment <byte>(new byte[bufferSize]);
                CancellationToken      token;
                WebSocketReceiveResult result = await _socket.ReceiveAsync(buffer, token);

                if (_socket.State == WebSocketState.Open)
                {
                    //当前数据大小
                    int curLength = Math.Min(buffer.Array.Length, result.Count);
                    //如果数据为json字符串,则为初次链接
                    if (result.MessageType == WebSocketMessageType.Text)
                    {
                        try
                        {
                            string    msg   = Encoding.UTF8.GetString(buffer.Array, 0, curLength);
                            UploadMsg upMsg = msg.JsonDeserialize <UploadMsg>();
                            if (upMsg == null)
                            {
                                throw new Exception("服务器接收客户json数据失败");
                            }
                            if (string.IsNullOrEmpty(this.SubFolder) == false)
                            {
                                upMsg.SubFolder = this.SubFolder + "/" + upMsg.SubFolder;
                            }
                            _file = new UploadInfo(upMsg);
                            //接收文件信息成功
                            await SendSuccess("接收文件信息成功");
                        }
                        catch (Exception ex)
                        {
                            await SendError(ex);
                        }
                    }
                    else if (result.MessageType == WebSocketMessageType.Binary)
                    {
                        try
                        {
                            //保存上传数据追加到文件
                            AppendFile(buffer, curLength);
                            curSize += curLength;
                            //相应服务器保存文件大小
                            await SendSuccess("服务器保存进行中...", curLength);
                        }
                        catch (Exception ex)
                        {
                            await SendError(ex);
                        }
                    }
                }
                else
                {
                    break;
                }
            }
        }
Exemple #12
0
        /// <summary>
        /// 接收文件,逻辑处理
        /// </summary>
        public void DoWork()
        {
            HttpRequest req = _Context.Request;
            //判断是否 有fileinfo,则代表创建文件
            string info = req.Form["fileinfo"];

            if (string.IsNullOrEmpty(info) == false)
            {
                //创建文件
                try
                {
                    UploadMsg upMsg = info.JsonDeserialize <UploadMsg>();
                    if (upMsg == null)
                    {
                        throw new Exception("服务器接收文件信息json数据失败");
                    }
                    if (string.IsNullOrEmpty(this.SubFolder) == false)
                    {
                        upMsg.SubFolder = this.SubFolder + "/" + upMsg.SubFolder;
                    }
                    this.file = new UploadInfo(upMsg);
                    //接收文件信息成功
                    SendSuccess("接收文件信息,并创建问价成功");
                }
                catch (Exception ex)
                {
                    SendError(ex);
                }
            }
            else
            {
                //接收文件
                var files = req.Form.Files;
                if (files.Count <= 0)
                {
                    throw new Exception("获取上传文件失败");
                }
                IFormFile _file    = files[0];
                string    backInfo = req.Form["backinfo"];
                if (string.IsNullOrEmpty(backInfo))
                {
                    throw new Exception("获取文件信息失败");
                }

                UploadMsg upMsg = backInfo.JsonDeserialize <UploadMsg>();
                if (upMsg == null)
                {
                    throw new Exception("服务器接收文件信息json数据失败");
                }
                if (string.IsNullOrEmpty(this.SubFolder) == false)
                {
                    upMsg.SubFolder = this.SubFolder + "/" + upMsg.SubFolder;
                }

                this.file = new UploadInfo(upMsg);
                //获取文件数据
                Stream stream = _file.OpenReadStream();
                try
                {
                    byte[] dataOne = new byte[stream.Length];
                    stream.Read(dataOne, 0, dataOne.Length);
                    AppendFile(dataOne);
                }
                finally
                {
                    stream.Close();
                }
            }
        }