public ActionResult UploadTempImportFile(HttpPostedFileBase file) { if (file == null) { return(Json(new ReturnResult { Success = false, Message = "请选择上传文件!" })); } if (_Request == null) { _Request = Request; } string message = string.Empty; try { string fileSize = FileOperateHelper.FileSize(file.ContentLength); string fileType = Path.GetExtension(file.FileName); //保存文件 UploadFileManager.httpContext = _Request.RequestContext.HttpContext; string filePath = UploadFileManager.SaveAs(file, string.Empty, "Temp"); if (string.IsNullOrWhiteSpace(filePath)) { message = "上传失败!"; } return(Json(new { Success = string.IsNullOrEmpty(message), Message = message, FilePath = filePath }, "text/plain")); } catch (Exception ex) { message = string.Format("上传失败,异常:{0}", ex.Message); return(Json(new ReturnResult { Success = false, Message = message }, "text/plain")); } }
public ActionResult UploadTempImage(HttpPostedFileBase file) { if (file == null) { return(Json(new ReturnResult { Success = false, Message = "请选择上传文件!" })); } if (_Request == null) { _Request = Request; } string message = string.Empty; string imgName = _Request["imgName"].ObjToStr(); try { string fileSize = FileOperateHelper.FileSize(file.ContentLength); string fileType = Path.GetExtension(file.FileName); //保存文件 UploadFileManager.httpContext = _Request.RequestContext.HttpContext; string filePath = UploadFileManager.SaveAs(file, string.Empty, "Temp", imgName); if (!string.IsNullOrEmpty(filePath) && filePath.Substring(0, 1) != "/") { filePath = "/" + filePath; } else { message = "临时图片保存失败!"; } return(Json(new { Success = string.IsNullOrEmpty(message), Message = message, FilePath = filePath }, "text/plain")); } catch (Exception ex) { message = string.Format("图片上传失败,原因:{0}", ex.Message); return(Json(new ReturnResult { Success = false, Message = message }, "text/plain")); } }
public ActionResult UploadAttachment(HttpPostedFileBase[] file, Guid?moduleId, Guid?id, bool isCreateSwf = false) { if (file == null || file.Where(o => o == null).Count() > 0) { return(Json(new ReturnResult { Success = false, Message = "请选择上传文件!" })); } if (_Request == null) { _Request = Request; } SetRequest(_Request); string attachType = _Request["attachType"].ObjToStr(); UploadFileManager.httpContext = _Request.RequestContext.HttpContext; string message = string.Empty; StringBuilder msg = new StringBuilder(); List <AttachFileInfo> fileMsg = new List <AttachFileInfo>(); foreach (var item in file) { try { string fileSize = FileOperateHelper.FileSize(item.ContentLength); string fileType = Path.GetExtension(item.FileName); string fileName = item.FileName; string pathFlag = "\\"; if (WebConfigHelper.GetAppSettingValue("IsLinux") == "true") { pathFlag = "/"; } int s = fileName.LastIndexOf(pathFlag); if (s >= 0) { fileName = fileName.Substring(s + 1); } //保存文件 string filePath = string.Empty; if (moduleId.HasValue) //表单附件 { filePath = UploadFileManager.SaveAs(item, "Attachment", "Temp"); } else { filePath = UploadFileManager.SaveAs(item, string.Empty); } filePath = filePath.StartsWith("~/") ? filePath : filePath.StartsWith("/") ? "~" + filePath : "~/" + filePath; //swf保存路径 string swfPath = string.Empty; //pdf保存路径 string pdfPath = string.Empty; if (isCreateSwf && !moduleId.HasValue) { //exe路径 string exePath = _Request.RequestContext.HttpContext.Server.MapPath("~/bin/SWFTools/pdf2swf.exe"); //bin路径 string binPath = _Request.RequestContext.HttpContext.Server.MapPath("~/bin/"); if (fileType.Equals(".doc") || fileType.Equals(".docx") || fileType.Equals(".xls") || fileType.Equals(".xlsx") || fileType.Equals(".ppt") || fileType.Equals(".pptx") || fileType.Equals(".pdf")) { //取pdf和swf路径 GetFilePath(out pdfPath, out swfPath); //参数 string[] obj = new string[] { fileType, _Request.RequestContext.HttpContext.Server.MapPath(filePath), _Request.RequestContext.HttpContext.Server.MapPath(pdfPath), _Request.RequestContext.HttpContext.Server.MapPath(swfPath), exePath, binPath }; CreateSwfFile(obj); } } fileMsg.Add(new AttachFileInfo() { AttachFile = filePath, PdfFile = pdfPath, SwfFile = swfPath, FileName = fileName, FileType = fileType, FileSize = fileSize, AttachType = attachType }); } catch (Exception ex) { msg.AppendLine(item.FileName + "上传失败:" + ex.Message); break; } } if (moduleId.HasValue && id.HasValue) //查看页面,直接保存附件 { return(SaveFormAttach(moduleId.Value, id.Value, JsonHelper.Serialize(fileMsg), true)); } return(Json(new { Success = string.IsNullOrEmpty(msg.ToString()), Message = string.IsNullOrEmpty(msg.ToString()) ? "上传成功" : msg.ToString(), FileMsg = fileMsg.Count > 0 ? JsonHelper.Serialize(fileMsg) : string.Empty }, "text/plain")); }
/// <summary> /// 上传附件,兼容非表单附件 /// </summary> /// <param name="file">文件</param> /// <returns></returns> public JsonResult UploadAttachment(IFormFileCollection file) { if (file == null || file.Count() == 0) { return(Json(new ReturnResult { Success = false, Message = "请选择上传文件!" })); } if (_Request == null) { _Request = Request; } SetRequest(_Request); Guid? moduleId = _Request.QueryEx("moduleId").ObjToGuidNull(); //模块Id,针对表单附件 Guid? id = _Request.QueryEx("id").ObjToGuidNull(); //记录Id,针对表单附件 bool isCreateSwf = _Request.QueryEx("isCreateSwf").ObjToBool(); //是否创建SWF文件 string attachType = _Request.QueryEx("attachType").ObjToStr(); //附件类型 string message = string.Empty; StringBuilder msg = new StringBuilder(); List <AttachFileInfo> fileMsg = new List <AttachFileInfo>(); foreach (var item in file) { try { string fileSize = FileOperateHelper.FileSize(item.Length); string fileType = Path.GetExtension(item.FileName); string fileName = item.FileName; string pathFlag = System.IO.Path.DirectorySeparatorChar.ToString(); int s = fileName.LastIndexOf(pathFlag); if (s >= 0) { fileName = fileName.Substring(s + 1); } //保存文件 string filePath = string.Empty; if (moduleId.HasValue) //表单附件 { filePath = UploadFileManager.SaveAs(item, "Attachment", "Temp"); } else { filePath = UploadFileManager.SaveAs(item, "Temp"); } filePath = filePath.StartsWith("~/") ? filePath : filePath.StartsWith("/") ? "~" + filePath : "~/" + filePath; //swf保存路径 string swfPath = string.Empty; //pdf保存路径 string pdfPath = string.Empty; if (isCreateSwf && !moduleId.HasValue) { //exe路径 string exePath = WebHelper.MapPath("~/bin/SWFTools/pdf2swf.exe"); //bin路径 string binPath = WebHelper.MapPath("~/bin/"); if (fileType.Equals(".doc") || fileType.Equals(".docx") || fileType.Equals(".xls") || fileType.Equals(".xlsx") || fileType.Equals(".ppt") || fileType.Equals(".pptx") || fileType.Equals(".pdf")) { //取pdf和swf路径 SystemOperate.GetSwfFilePath(out pdfPath, out swfPath); //参数 string[] obj = new string[] { fileType, WebHelper.MapPath(filePath), WebHelper.MapPath(pdfPath), WebHelper.MapPath(swfPath), exePath, binPath }; SystemOperate.CreateSwfFile(obj); } } fileMsg.Add(new AttachFileInfo() { AttachFile = filePath, PdfFile = pdfPath, SwfFile = swfPath, FileName = fileName, FileType = fileType, FileSize = fileSize, AttachType = attachType }); } catch (Exception ex) { msg.AppendLine(item.FileName + "上传失败:" + ex.Message); break; } } if (moduleId.HasValue && moduleId.Value != Guid.Empty && id.HasValue && id.Value != Guid.Empty) //查看页面,直接保存附件 { return(SaveFormAttach(moduleId.Value, id.Value, JsonHelper.Serialize(fileMsg), true)); } return(Json(new { Success = string.IsNullOrEmpty(msg.ToString()), Message = string.IsNullOrEmpty(msg.ToString()) ? "上传成功" : msg.ToString(), FileMsg = fileMsg.Count > 0 ? JsonHelper.Serialize(fileMsg) : string.Empty })); }