/// <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(); } } }
/// <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); } }
/// <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(); } }
/// <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); } }
/// <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(); } }
/// <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; } } }
/// <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(); } } }