private void FileConversion(ref TbAttachment fileEntity, string mapPath, string name) { //转换pdf路径 var fileConfig = System.Configuration.ConfigurationManager.AppSettings["uploadFileChange"] ?? ""; var fielConfigCreate = Server.MapPath("/" + fileConfig); if (!Directory.Exists(fielConfigCreate)) { Directory.CreateDirectory(fielConfigCreate); //创建文件夹 } string fileUrl = string.Format("/{0}/{1}.pdf", fileConfig, name); string path = Server.MapPath(fileUrl); if (fileEntity.FileStoragePath.ToUpper().Contains(".DOC") || fileEntity.FileStoragePath.ToUpper().Contains(".DOCX")) { if (!System.IO.File.Exists(path))//判断文件是否存在 { //读取doc文档 Aspose.Words.Document doc = new Aspose.Words.Document(mapPath); //保存为PDF文件,此处的SaveFormat支持很多种格式,如图片,epub,rtf 等等 doc.Save(path, Aspose.Words.SaveFormat.Pdf); } fileEntity.FileStoragePath = fileUrl; } else if (fileEntity.FileStoragePath.ToUpper().Contains(".XLS") || fileEntity.FileStoragePath.ToUpper().Contains(".XLSX")) { if (!System.IO.File.Exists(path)) { Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(mapPath); wb.Save(path, Aspose.Cells.SaveFormat.Pdf); } fileEntity.FileStoragePath = fileUrl; } else if (fileEntity.FileStoragePath.ToUpper().Contains(".TXT")) { string[] lines = System.IO.File.ReadAllLines(urlconvertorlocal(fileEntity.FileStoragePath), Encoding.Default); ViewData["c_txt"] = lines; } else if (fileEntity.FileStoragePath.ToUpper().Contains(".JPG") || fileEntity.FileStoragePath.ToUpper().Contains(".PNG") || fileEntity.FileStoragePath.ToUpper().Contains(".GIF") || fileEntity.FileStoragePath.ToUpper().Contains(".JPEG")) { if (!System.IO.File.Exists(path))//判断文件是否存在 { ConvertImageToPdf(mapPath, path); } fileEntity.FileStoragePath = fileUrl; } }
/// <summary> /// 新增数据(单条) /// </summary> public AjaxResult Insert(TbAttachment request) { if (request == null) { return(AjaxResult.Warning("参数错误")); } try { var model = MapperHelper.Map <TbAttachment, TbAttachment>(request); var count = Repository <TbAttachment> .Insert(model, true); if (count > 0) { return(AjaxResult.Success("上传成功")); } return(AjaxResult.Error()); } catch (Exception) { return(AjaxResult.Error()); } }
/// <summary> /// 上传图片(多张) /// </summary> /// <param name="quePara">list集合</param> /// <returns></returns> public string UploadFile(string Enclosure, string UserCode) { string str = ""; try { List <TbAttachment> list = new List <TbAttachment>(); JavaScriptSerializer Serializers = new JavaScriptSerializer(); //json字符串转为数组对象, 反序列化 List <File> quePara = Serializers.Deserialize <List <File> >(Enclosure); foreach (var item in quePara) { int FileSize = 0; string file = item.file == null ? "" : item.file.ToString(); string fileName = item.fileName == null ? "" : item.fileName.ToString(); byte[] bt = Convert.FromBase64String(file); FileSize = bt.Length; MemoryStream stream = new MemoryStream(bt); Bitmap bitmap = new Bitmap(stream); string saveTempPath = "/UploadFile"; string resTempPath = "http://" + HostAddress + "/UploadFile"; string dirTempPath = HostingEnvironment.MapPath(saveTempPath); if (!Directory.Exists(dirTempPath)) { Directory.CreateDirectory(dirTempPath); } fileName = Guid.NewGuid().ToString() + fileName; resTempPath += "/" + fileName; dirTempPath += "/" + fileName; bitmap.Save(dirTempPath); #region 文件保存成功后,写入附件的数据库记录 TbAttachment attachmentInfo = new TbAttachment(); attachmentInfo.FileID = Guid.NewGuid().ToString(); attachmentInfo.FunModule = "test"; attachmentInfo.FileType = "application/octet-stream"; attachmentInfo.FileSize = FileSize; attachmentInfo.FileName = fileName; attachmentInfo.FileModified = DateTime.Now.ToString(); attachmentInfo.StartTime = DateTime.Now; attachmentInfo.LastTime = DateTime.Now; attachmentInfo.FileStoragePath = dirTempPath; attachmentInfo.UserCode = UserCode; attachmentInfo.StorageOver = 0; attachmentInfo.StorageSize = 0; attachmentInfo.ServerCode = ""; attachmentInfo.FileToKen = ""; list.Add(attachmentInfo); #endregion str += attachmentInfo.FileID + ","; } AttachmentInsert(list, true); return(str); } catch (Exception ex) { return(""); } }
/// <summary> /// 文件上传 /// </summary> /// <returns></returns> public ActionResult UplaodFile(string keyID, string menuTable, string DataId) { if (!string.IsNullOrWhiteSpace(keyID)) { var fileContext = SaveFiles(); if (fileContext.Count > 0) { foreach (var item in fileContext) { var entity = new TbAttachment { FileID = keyID, FunModule = "test", FileType = item.Format, FileSize = Convert.ToInt32(item.Size), FileName = item.Name, FileModified = DateTime.Now.ToString(), StartTime = DateTime.Now, LastTime = DateTime.Now, FileStoragePath = item.Url, UserCode = OperatorProvider.Provider.CurrentUser.UserCode, StorageOver = 0, StorageSize = 0, ServerCode = "", FileToKen = "", }; if (!string.IsNullOrWhiteSpace(DataId)) { var anyRet = _attachmentImp.AnyInfo(menuTable, Convert.ToInt32(DataId)); if (anyRet != null && anyRet.Rows.Count > 0) { //保存业务表中附件字段 bool flag = _attachmentImp.SaveDataFile(keyID, menuTable, Convert.ToInt32(DataId)); if (flag) { //将上传的相关信息存入数据中 Repository <TbAttachment> .Insert(entity); } } else { //将上传的相关信息存入数据中 Repository <TbAttachment> .Insert(entity); } } else { //将上传的相关信息存入数据中 Repository <TbAttachment> .Insert(entity); } //获取上传附件的ID var model = Repository <TbAttachment> .GetAll().Where(d => d.FileID == keyID).OrderByDescending(d => d.id).FirstOrDefault(); item.ServerFileID = Convert.ToString(model.id); item.keyID = keyID; } } return(Json(fileContext)); } else { return(Json("")); } }
/// <summary> /// 上传 /// </summary> /// <param name="Filedata"></param> /// <returns></returns> public ActionResult UploadifyFun(HttpPostedFileBase Filedata, string UserCode) { if (Filedata == null || String.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == 0) { return(this.HttpNotFound()); } string savePath = Server.MapPath("/") + "UploadFile\\";//保存文件地址 if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } #region 生成4位随机数 string vc = ""; Random rNum = new Random(); //随机生成类 int num1 = rNum.Next(0, 9); //返回指定范围内的随机数 int num2 = rNum.Next(0, 9); int num3 = rNum.Next(0, 9); int num4 = rNum.Next(0, 9); int[] nums = new int[4] { num1, num2, num3, num4 }; for (int i = 0; i < nums.Length; i++)//循环添加四个随机生成数 { vc += nums[i].ToString(); } #endregion string filename = System.IO.Path.GetFileName(vc + Filedata.FileName); string virtualPath = String.Format(savePath + filename); Filedata.SaveAs(virtualPath); bool uploaded = System.IO.File.Exists(virtualPath); if (uploaded) { #region 文件保存成功后,写入附件的数据库记录 TbAttachment attachmentInfo = new TbAttachment(); attachmentInfo.FileID = Guid.NewGuid().ToString(); attachmentInfo.FunModule = "test"; attachmentInfo.FileType = Filedata.ContentType; attachmentInfo.FileSize = long.Parse(FormatFileSize(Convert.ToInt64(Filedata.ContentLength))); attachmentInfo.FileName = filename; attachmentInfo.FileModified = DateTime.Now.ToString(); attachmentInfo.StartTime = DateTime.Now; attachmentInfo.LastTime = DateTime.Now; attachmentInfo.FileStoragePath = virtualPath; attachmentInfo.UserCode = UserCode; attachmentInfo.StorageOver = 0; attachmentInfo.ServerCode = ""; attachmentInfo.FileToKen = ""; var data = _attachmentImp.Insert(attachmentInfo); return(Content(attachmentInfo.FileID)); #endregion } else { return(Content("上传失败")); } }
/// <summary> /// 附件上传 /// </summary> /// <param name="Filedata"></param> /// <param name="UserCode"></param> /// <returns></returns> public string UploadFile(HttpPostedFileBase Filedata, string UserCode) { var returnstr = new { success = "false", data = "", message = "" }; //第一步创建路径 if (Filedata != null) { try { string savePath = Server.MapPath("/") + "UploadFile\\";//保存文件地址 if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } #region 生成4位随机数 string vc = ""; Random rNum = new Random(); //随机生成类 int num1 = rNum.Next(0, 9); //返回指定范围内的随机数 int num2 = rNum.Next(0, 9); int num3 = rNum.Next(0, 9); int num4 = rNum.Next(0, 9); int[] nums = new int[4] { num1, num2, num3, num4 }; for (int i = 0; i < nums.Length; i++)//循环添加四个随机生成数 { vc += nums[i].ToString(); } #endregion string filename = System.IO.Path.GetFileName(vc + Filedata.FileName); string virtualPath = String.Format(savePath + filename); //将文件“拷贝”到指定路径 try { Filedata.SaveAs(virtualPath); try { bool uploaded = System.IO.File.Exists(virtualPath); if (uploaded) { #region 文件保存成功后,写入附件的数据库记录 TbAttachment attachmentInfo = new TbAttachment(); attachmentInfo.FileID = Guid.NewGuid().ToString(); attachmentInfo.FunModule = "test"; attachmentInfo.FileType = Filedata.ContentType; //attachmentInfo.FileSize = long.Parse(FormatFileSize(Convert.ToInt64(Filedata.ContentLength))); attachmentInfo.FileSize = Filedata.ContentLength; attachmentInfo.FileName = filename; attachmentInfo.FileModified = DateTime.Now.ToString(); attachmentInfo.StartTime = DateTime.Now; attachmentInfo.LastTime = DateTime.Now; attachmentInfo.FileStoragePath = virtualPath; attachmentInfo.UserCode = UserCode; attachmentInfo.StorageOver = 0; attachmentInfo.ServerCode = ""; attachmentInfo.FileToKen = ""; var data = _attachmentImp.Insert(attachmentInfo); returnstr = new { success = "true", data = attachmentInfo.FileID, message = "文档上传成功!" }; return(new JavaScriptSerializer().Serialize(returnstr)); #endregion } else { returnstr = new { success = "false", data = "", message = "上传文件过程中发生异常" }; return(new JavaScriptSerializer().Serialize(returnstr)); } } catch (Exception e) { //删掉已经上传的文件 System.IO.File.Delete(virtualPath); returnstr = new { success = "false", data = "", message = "连接文档服务器OK!调用SaveAs方法过程中发生异常,异常信息如下:" + e.Message }; return(new JavaScriptSerializer().Serialize(returnstr)); } } catch (System.Exception e) { //删掉已经上传的文件 System.IO.File.Delete(virtualPath); returnstr = new { success = "false", data = "", message = "连接文档服务器OK!调用SaveAs方法过程中发生异常,异常信息如下:" + e.Message }; return(new JavaScriptSerializer().Serialize(returnstr)); } } catch (System.Exception ex) { returnstr = new { success = "false", data = "", message = "上传文件过程中发生异常,异常详情如下:" + ex.Message }; return(new JavaScriptSerializer().Serialize(returnstr)); } } else { return(new JavaScriptSerializer().Serialize(returnstr)); } }
public Tuple <string, string> UploadFileData(List <FileData> files, bool inDatabase = false) { try { string UrlPaths = ""; string fileId = ""; if (files.Count > 0) { fileId = Guid.NewGuid().ToString(); } string saveTempPath = "/UploadFile"; string resTempPath = "http://" + HostAddress + saveTempPath; string dirTempPath = System.Web.Hosting.HostingEnvironment.MapPath(saveTempPath); foreach (var item in files) { string fname = item.fileName; string path = dirTempPath; byte[] bt = Convert.FromBase64String(item.file); System.IO.MemoryStream stream = new System.IO.MemoryStream(bt); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(stream); if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } item.fileName = Guid.NewGuid().ToString() + item.fileName; resTempPath += "/" + item.fileName; path += "/" + item.fileName; bitmap.Save(path); if (inDatabase) { var info = GetFileInfoByPath(path, fname); if (info == null) { return(null); } #region 文件保存成功后,写入附件的数据库记录 TbAttachment attachmentInfo = new TbAttachment(); attachmentInfo.FileID = fileId; attachmentInfo.FunModule = "test"; attachmentInfo.FileType = "application/octet-stream"; attachmentInfo.FileSize = info.Item1; attachmentInfo.FileName = info.Item2; attachmentInfo.FileModified = DateTime.Now.ToString(); attachmentInfo.StartTime = DateTime.Now; attachmentInfo.LastTime = DateTime.Now; attachmentInfo.FileStoragePath = saveTempPath + "/" + item.fileName; attachmentInfo.UserCode = UserCode; attachmentInfo.StorageOver = 0; attachmentInfo.ServerCode = ""; attachmentInfo.FileToKen = ""; var data = _attachmentImp.Insert(attachmentInfo); //fileId += attachmentInfo.FileID + ","; #endregion } UrlPaths += resTempPath + ","; } UrlPaths = UrlPaths.TrimEnd(','); //fileId = fileId.TrimEnd(','); return(new Tuple <string, string>(UrlPaths, fileId)); } catch (Exception ex) { return(null); } }