You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
7.7 KiB
C#

using System;
using HcUtility.Core;
using Newtonsoft.Json;
using DSWeb.Areas.CommMng.DAL;
using System.Web.Mvc;
using System.IO;
using DSWeb.MvcShipping.Helper;
namespace DSWeb.Areas.CommMng.Models
{
[JsonObject]
//用于解析要向表中保存的url地址的相关信息
public class FileUrlModel
{
#region
private string _TableName = string.Empty;
private string _KeyName = string.Empty;
private string _KeyValue = string.Empty;
private string _UrlName = string.Empty;
private string _UrlValue = string.Empty;
private string _ImgName = string.Empty;
private string _INDATABASE = string.Empty;
private string _FilePath = string.Empty;
private string ModelName = "";
private string ChildName = "";
private string ExName = "";//扩展名 在savefile时获取
#endregion
#region
public string TableName
{
get { return _TableName; }
set { _TableName = value; }
}
public string KeyName
{
get { return _KeyName; }
set { _KeyName = value; }
}
public string KeyValue
{
get { return _KeyValue; }
set { _KeyValue = value; }
}
public string UrlName
{
get { return _UrlName; }
set { _UrlName = value; }
}
public string UrlValue
{
get { return _UrlValue; }
set { _UrlValue = value; }
}
public string ImgName
{
get { return _ImgName; }
set { _ImgName = value; }
}
public string INDATABASE
{
get { return _INDATABASE; }
set { _INDATABASE = value; }
}
public bool needintoDatabase {
get {
if (_INDATABASE == "True" || _INDATABASE == "true") {
return true;
}return false;
}
}
public string FilePath
{
get { return _FilePath; }
set { _FilePath = value; }
}
public string Savefilename
{
get {
var _r = Savefilename_head;
_r += ExName;
return _r;
}
}
public string Savefilename_head
{
get
{
var _r = KeyValue;
if (!String.IsNullOrEmpty(ChildName))
{
_r += "_" + ChildName;
}
return _r;
}
}
#endregion
public static string _urlbase= "../../UploadFiles";
private string getSQL() {
var sql = String.Format("update {0} set {1}='{2}' where {3}='{4}'", TableName, UrlName, UrlValue, KeyName, KeyValue);
return sql;
}
public int DoUpdate() {
var _r = BasicDataRefDAL.ExecSql(getSQL());
return _r;
}
public FileUrlModel() {
}
public FileUrlModel(string ModelName)
{
var path = System.Web.HttpContext.Current.Server.MapPath(FileUrlModel._urlbase);
setModelValue(ModelName, path);
}
public FileUrlModel(string head,string ModelName,string ChildName="") {
var _Info = DSWeb.MvcShipping.Helper.JsonConvert.Deserialize<FileUrlModel>(head);
var path = System.Web.HttpContext.Current.Server.MapPath(FileUrlModel._urlbase);
setModelValue(ModelName, path, ChildName);
//ChildName用于表示在某一模块下的多种类型文件 如info_client(往来单位)下添加电子章 或其他种类图片
this.SetValue(_Info);
}
private void setModelValue(string ModelName,string path, string ChildName = "") {
if (ModelName == "FactoryCargoname_TAGPIC")
{//进口贸易-厂号商品名称管理
TableName = "Import_Cargoname";
KeyName = "GID";
UrlName = "TAGPICURL";
FilePath = path + "\\" + ModelName;
this.ModelName = ModelName;
this.ChildName = ChildName;
}
if (ModelName == "InfoClient_Img")
{//往来单位-图片管理
//需要写入数据库
TableName = "Info_Client_IMG";
KeyName = "GID";
UrlName = "IMGURL";
ImgName = "IMG";
FilePath = path + "\\" + ModelName;
this.ModelName = ModelName;
this.ChildName = ChildName;
//INDATABASE = true;
}
}
public void SetValue(FileUrlModel info)
{
if (!string.IsNullOrEmpty(info.TableName))
{
TableName = info.TableName;
}
if (!string.IsNullOrEmpty(info.KeyName))
{
KeyName = info.KeyName;
}
if (!string.IsNullOrEmpty(info.UrlName))
{
UrlName = info.UrlName;
}
if (!string.IsNullOrEmpty(info.KeyValue))
{
KeyValue = info.KeyValue;
}
if (!string.IsNullOrEmpty(info.UrlValue))
{
UrlValue = info.UrlValue;
}
if (!string.IsNullOrEmpty(info.INDATABASE))
{
INDATABASE = info.INDATABASE;
}
}
private static string getexname(System.Web.HttpPostedFileBase file) {
return Path.GetExtension(file.FileName);
}
private string getFileName(System.Web.HttpPostedFileBase file) {
var _r = FilePath + "\\" + Savefilename;
return _r;
}
/// <summary>
/// 保存方法 返回值为文件url
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public string SaveFile(System.Web.HttpPostedFileBase file)
{
//获得文件扩展名
ExName = getexname(file);
//首先 如没有目录则建立目录
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
string filename = getFileName(file);
//如存在同名文件则删除
string[] fileNames = Directory.GetFiles(FilePath);
foreach (string _file in fileNames)
{
if (_file.IndexOf(Savefilename_head, StringComparison.OrdinalIgnoreCase) >= 0) {
File.Delete(_file);
}
}
//if (File.Exists(filename))
//{
// File.Delete(filename);
//}
//保存文件
file.SaveAs(filename);
if (File.Exists(filename))
{
//向数据库写入url
UrlValue = _urlbase + "/" + ModelName + "/" + Savefilename;
DoUpdate();
//调取升级数据库内图片字段的方法
if (needintoDatabase) {
BasicDataRefDAL.SaveFileIntoDB(this, UrlValue);
}
//返回是否存在此文件
return UrlValue;
}
else return "";
}
}
[JsonObject]
public class PrintSql
{
public string sql1 { get; set; }
public string sql2 { get; set; }
public string sql3 { get; set; }
public string sql4 { get; set; }
public string sql5 { get; set; }
public string sql6 { get; set; }
public string sql7 { get; set; }
public string sql8 { get; set; }
}
}