public static bool UploadFile(Stream stream, string fileName) { FileTransferMessage fileMessage = new FileTransferMessage(); fileMessage.CookieStr = GlobalStaticObj.CookieStr; fileMessage.UserID = GlobalStaticObj.UserID; //fileMessage.SavePath = filePath; fileMessage.FileName = fileName; //FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); fileMessage.FileData = stream; try { return GlobalStaticObj.proxyFile.UploadFile(fileMessage).Flag; //GlobalStaticObj.proxyFile.UploadFile1(stream, fileName); } catch (Exception e) { return false; } }
/// <summary> /// 上传文件 /// </summary> /// <returns></returns> public static bool UploadFile(string savePath, string fileName) { FileTransferMessage fileMessage = new FileTransferMessage(); fileMessage.CookieStr = GlobalStaticObj.CookieStr; fileMessage.UserID = GlobalStaticObj.UserID; fileMessage.SavePath = savePath; fileMessage.FileName = fileName; FileStream fileStream = new FileStream(savePath, FileMode.Open, FileAccess.Read); fileMessage.FileData = fileStream; try { GlobalStaticObj.proxyFile.UploadFile(fileMessage); fileStream.Close(); fileStream.Dispose(); return true; } catch (Exception e) { return false; } }
/// <summary> /// 上传附件到指定路径 /// </summary> /// <param name="path">指定服务端路径</param> /// <param name="filePath">本地文件路径</param> /// <returns></returns> public static bool UploadFileByPath(string path, string filePath) { FileTransferMessage fileMessage = new FileTransferMessage(); fileMessage.CookieStr = GlobalStaticObj.CookieStr; fileMessage.UserID = GlobalStaticObj.UserID; fileMessage.SavePath = path; fileMessage.FileName = Path.GetFileName(filePath); FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); fileMessage.FileData = fileStream; try { GlobalStaticObj.proxyFile.UploadFile(fileMessage); fileStream.Close(); fileStream.Dispose(); return true; } catch (Exception e) { fileStream.Close(); fileStream.Dispose(); GlobalStaticObj.GlobalLogService.WriteLog("【附件上传】" + e); //Modify by kord -- Utility.Log.Log.writeLineToLog(e, "附件上传"); return false; } }
/// <summary> 上传文件 /// </summary> /// <param name="request">1上传成功,0上传失败</param> public UploadFileResponse UploadFile(FileTransferMessage request) { UploadFileResponse response = new UploadFileResponse(); response.Flag = false; UserFileOPLog uOpLog = new UserFileOPLog(); UserIDOP uOp = new UserIDOP(); uOp.UserID = request.UserID; uOp.OPName = "UploadFile"; uOpLog.userOP = uOp; uOpLog.FileName = request.FileName; if (!Directory.Exists(GlobalStaticObj_Server.Instance.FilePath))//存放的默认文件夹是否存在 { Directory.CreateDirectory(GlobalStaticObj_Server.Instance.FilePath);//不存在则创建 } string path = GlobalStaticObj_Server.Instance.FilePath; if (!string.IsNullOrEmpty(request.SavePath)) { path = Path.Combine(GlobalStaticObj_Server.Instance.FilePath, request.SavePath); } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } uOpLog.sTimeTicks = DateTime.UtcNow.Ticks; string fileName = request.FileName;//文件名 string fileFullPath = Path.Combine(path, fileName);//合并路径生成文件存放路径 uOpLog.FilePath = fileFullPath; Stream sourceStream = request.FileData; if (sourceStream == null) { return response; } if (!sourceStream.CanRead) { return response; } //创建文件流,读取流中的数据生成文件 using (FileStream fs = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write, FileShare.None)) { try { const int bufferLength = 4096; byte[] myBuffer = new byte[bufferLength];//数据缓冲区 int count; while ((count = sourceStream.Read(myBuffer, 0, bufferLength)) > 0) { fs.Write(myBuffer, 0, count); } fs.Close(); sourceStream.Close(); uOpLog.eTimeTicks = DateTime.UtcNow.Ticks; uOpLog.exeResult = true; response.Flag = true; return response; } catch (Exception ex) { uOpLog.eTimeTicks = DateTime.UtcNow.Ticks; uOpLog.exeResult = false; return response; throw ex; } finally { writeUserFileOpLog(uOpLog); } } }