123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System;
- using SysBaseLibs;
- using System.Web;
- using System.IO;
- namespace SysDataLibs.TableClass
- {
- public partial class Sys_AttachFiles_info
- {
- public bool UpdateFile(HttpFileCollection poFileUpload, UserSession poSession,bool isInsert=true)
- {
- //检查要上传的文件是否存在
- if (poFileUpload.Count <= 0)
- return false;
- bool lbRetVal = false;
- string uploadFileName = poFileUpload[0].FileName;
- if (uploadFileName.Trim() != "")
- {
- try
- {
- if (MyUtils.IsValidFileType(uploadFileName))
- {
- uploadFileName = uploadFileName.Substring(uploadFileName.LastIndexOf("\\", StringComparison.Ordinal) + 1);
- FileExt = uploadFileName.Substring(uploadFileName.LastIndexOf(".", StringComparison.Ordinal) + 1, uploadFileName.Length - uploadFileName.LastIndexOf(".", StringComparison.Ordinal) - 1);
- if (string.IsNullOrEmpty(FileTitle))
- FileTitle = uploadFileName.Remove(uploadFileName.LastIndexOf(".", StringComparison.Ordinal));
- FilePath = AppEnv.SysSetObj.GetString("DOWNLOADPATH") + "/" + AppEnv.SysSetObj.GetString("ATTACHFILES") + "/" + TableId.ToUpper();
- var uploadFileDestination = HttpContext.Current.Request.PhysicalApplicationPath;
- uploadFileDestination += FilePath;
- if (!Directory.Exists(uploadFileDestination))
- {
- Directory.CreateDirectory(uploadFileDestination);
- }
- string lcDateRd = FileTitle + "-" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + "." + FileExt;
- FileName = lcDateRd.Replace(" ", "");
- poFileUpload[0].SaveAs(uploadFileDestination + "/" + FileName);
- string s = isInsert ? InsertSql() : UpdateSql();
- lbRetVal = poSession.DBConn.ExcuteSqlTran(s);
- ThreadLog.LogInfo(poSession.UserInfo.Names + "(" + poSession.UserInfo.UserID + ")" + " Update File " + DownPath());
- //lbRetVal = true;
- }
- }
- catch (Exception e)
- {
- ThreadLog.LogException(e);
- ThreadLog.LogErr("文件上传异常");
- ThreadLog.LogException(e);
- lbRetVal = false;
- }
- }
- return lbRetVal;
- }
- public bool UpdateFileBase64(string base64Str, UserSession poSession, bool isInsert = true)
- {
- //检查要上传的文件是否存在
-
- bool lbRetVal = false;
- string uploadFileName = FileName;
- if (uploadFileName.Trim() != "")
- {
- try
- {
- FilePath = AppEnv.SysSetObj.GetString("DOWNLOADPATH") + "/" + AppEnv.SysSetObj.GetString("ATTACHFILES") + "/" + TableId.ToUpper();
- string fName = $"{FileName}-{DateTime.Now:yyMMddHHmmss}{new Random().Next(1000, 9999)}";
- var lcRetVal = Base64ToFile(base64Str, fName, FileExt, FilePath);
- // poFileUpload[0].SaveAs(uploadFileDestination + "/" + FileName);
- if (lcRetVal.StartsWith("error@"))
- {
- ThreadLog.LogInfo((lcRetVal.Split(new[] { '@' },
- StringSplitOptions.RemoveEmptyEntries)[1]));
- return false;
- }
- FileName = $"{fName}.{FileExt}";
- string s = isInsert ? InsertSql() : UpdateSql();
- lbRetVal = poSession.DBConn.ExcuteSqlTran(s);
- ThreadLog.LogInfo(poSession.UserInfo.Names + "(" + poSession.UserInfo.UserID + ")" + " Update File " + DownPath());
- //lbRetVal = true;
- }
- catch (Exception e)
- {
- ThreadLog.LogException(e);
- ThreadLog.LogErr("文件上传异常");
- ThreadLog.LogException(e);
- lbRetVal = false;
- }
- }
- return lbRetVal;
- }
- public static string Base64ToFile(string base64Str, string fileName, string fileExt, string filePath)
- {
- string lcRetVal = "error@";
- try
- {
- fileName = $"{fileName}.{fileExt}";
- filePath = filePath.StartsWith("/") ? filePath : ("/" + filePath);
- filePath = filePath.EndsWith("/") ? filePath : (filePath + "/");
- string path = $"{AppDomain.CurrentDomain.BaseDirectory}{filePath}";
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- byte[] bytes = Convert.FromBase64String(base64Str);
- using (FileStream fs = new FileStream($"{path}{fileName}", FileMode.Create, FileAccess.Write))
- {
- fs.Write(bytes, 0, bytes.Length);
- fs.Close();
- }
- lcRetVal = filePath + fileName;
- }
- catch (Exception e)
- {
- //typeof(SysAttachFileAppService).LogError(e);
- lcRetVal += "文件上传异常。";
- }
- return lcRetVal;
- }
- }
-
- }
|