using Lskj.Business; using Lskj.Control.BrowserSetting; using Lskj.PubPdfSign.Model; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Windows.Forms; namespace Lskj.PubPdfSign { public static class FileUploadUtil { /// /// 上传文件 /// /// /// public static bool UploadFile(string filePath, DynamicPdfSignModel PdfSignModel, ref UploadModel uploadModel) { bool isUpload = false; string postUrl = ""; try { //获取文件信息 FileInfo fileInfo = new FileInfo(filePath); byte[] fileBytes = File.ReadAllBytes(filePath); string fileBase64String = Convert.ToBase64String(fileBytes); string fileName = Guid.NewGuid().ToString().Replace("-", "") + "_" + uploadModel.FileName; //上传文件到服务器目录 postUrl = "{0}Api/FileUploadApi.ashx?moduleId={1}&folder={2}&totsize={3}&position={4}&filename={5}&method={6}"; postUrl = string.Format(postUrl, SystemInfo.Instance.OAUrl, PdfSignModel.ModuleCode, "PdfSignTemp", fileInfo.Length, 0, fileName, "DoWebUpload"); HttpTools.setting("application/x-www-form-urlencoded", null, null); string result = ""; CookieCollection cookieCollection = null; HttpWebResponse webResponse = HttpTools.Post(postUrl, fileBase64String, null, HttpTools.Method.POST, out cookieCollection, out result); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject jsonObject = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result); string isSuccess = jsonObject["success"] + ""; if (isSuccess.Equals("True", StringComparison.CurrentCultureIgnoreCase)) { uploadModel.FileId = jsonObject["other"] + ""; uploadModel.Webpath = jsonObject["data"]["WebRelPath"] + ""; isUpload = true; } else { if (jsonObject.ContainsKey("msg")) { uploadModel.Msg = jsonObject["msg"] + ""; } isUpload = false; } } else { uploadModel.Msg = "上传请求失败"; isUpload = false; } } catch (Exception ex) { uploadModel.Msg = ex.Message; isUpload = false; } return isUpload; } public static bool UploadFileToServer(string url, string filePath, DynamicPdfSignModel PdfSignModel, ref UploadModel uploadModel) { bool isUpload = false; string postUrl = ""; try { //获取文件信息 FileInfo fileInfo = new FileInfo(filePath); byte[] fileBytes = File.ReadAllBytes(filePath); string fileBase64String = Convert.ToBase64String(fileBytes); //上传文件到服务器目录 postUrl = "{0}Api/FileUploadApi.ashx?moduleId={1}&folder={2}&totsize={3}&position={4}&filename={5}&speciesno={6}&method={7}"; postUrl = string.Format(postUrl, url, PdfSignModel.ModuleCode, "file", fileInfo.Length, 0, uploadModel.FileName, uploadModel.SpeciesNo, "DoWebUpload"); HttpTools.setting("application/x-www-form-urlencoded", null, null); string result = ""; CookieCollection cookieCollection = null; HttpWebResponse webResponse = HttpTools.Post(postUrl, fileBase64String, null, HttpTools.Method.POST, out cookieCollection, out result); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject jsonObject = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result); string isSuccess = jsonObject["success"] + ""; if (isSuccess.Equals("True", StringComparison.CurrentCultureIgnoreCase)) { uploadModel.FileId = jsonObject["other"] + ""; uploadModel.Webpath = jsonObject["data"]["WebRelPath"] + ""; isUpload = true; } else { if (jsonObject.ContainsKey("msg")) { uploadModel.Msg = jsonObject["msg"] + ""; } isUpload = false; } } else { uploadModel.Msg = "上传请求失败"; isUpload = false; } } catch (Exception ex) { uploadModel.Msg = ex.Message; isUpload = false; } return isUpload; } /// /// /// /// /// /// /// /// public static bool MoveFileTo(string url, string fileId, string filepath, string newdir, DynamicPdfSignModel PdfSignModel, ref UploadModel uploadModel) { bool isUpload = false; string postUrl = ""; try { //上传文件到服务器目录 postUrl = "{0}Api/FileUploadApi.ashx?moduleId={1}&filepath={2}&newdir={3}&userid={4}&username={5}&fileid={6}&method={7}"; postUrl = string.Format(postUrl, url, PdfSignModel.ModuleCode, filepath, newdir, "1", "管理员", fileId, "MoveFileTo"); HttpTools.setting("application/x-www-form-urlencoded", null, null); string result = ""; CookieCollection cookieCollection = null; HttpWebResponse webResponse = HttpTools.Post(postUrl, "", null, HttpTools.Method.POST, out cookieCollection, out result); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject jsonObject = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result); string isSuccess = jsonObject["success"] + ""; if (isSuccess.Equals("True", StringComparison.CurrentCultureIgnoreCase)) { uploadModel.FileId = jsonObject.ContainsKey("other") ? jsonObject["other"] + "" : ""; isUpload = true; } else { if (jsonObject.ContainsKey("msg")) { uploadModel.Msg = jsonObject["msg"] + ""; } isUpload = false; } } else { uploadModel.Msg = "移动请求失败"; isUpload = false; } } catch (Exception ex) { uploadModel.Msg = ex.Message; isUpload = false; } return isUpload; } /// /// 删除文件 /// /// /// public static bool DeleteFile(UploadModel uploadModel) { bool isDelete = false; try { //删除文件 string postUrl = "{0}Api/FileUploadApi.ashx?fileId={1}&method={2}"; postUrl = string.Format(postUrl, SystemInfo.Instance.OAUrl, uploadModel.FileId, "DoDelete"); HttpTools.setting("application/x-www-form-urlencoded", null, null); string result = ""; CookieCollection cookieCollection = null; HttpWebResponse webResponse = HttpTools.Post(postUrl, "", null, HttpTools.Method.POST, out cookieCollection, out result); if (webResponse != null && !string.IsNullOrEmpty(result)) { JObject jsonObject = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result); string isSuccess = jsonObject["success"] + ""; if (isSuccess.Equals("True", StringComparison.CurrentCultureIgnoreCase)) { isDelete = true; } else { isDelete = false; } } else { isDelete = false; } } catch (Exception) { isDelete = false; } return isDelete; } } }