//后台
public JsonResult upload_img_Add1(string id1)
{
lock (lock_tx)
{
string code = "";
string src = "";
string msg = "";
string name = "";
List
try
{
int id2 = 0;
int.TryParse(id1, out id2);
var item = Common.freeSqlHelper.fsqlDb.Select
if (item == null)
{
code = "0";
msg = "未能找到该会议!";
}
else
{
string userid = get_userid();
if (userid == "")
{
code = "0";
msg = "未找到当前登录用户!";
}
else
{
string xm = get_name1(userid);
var u2 = Common.freeSqlHelper.fsqlDb.Select
if (u2 == null)
{
code = "0";
msg = "未找到该用户!";
}
else
{
var files = Request.Files; //获取选中文件
for(int i = 0; i < files.Count; i++)
{
try
{
var file = files[i];
var filecombin = file.FileName.Split('.');
string houzhui = filecombin[filecombin.Length - 1];
if (file == null || String.IsNullOrEmpty(file.FileName) || file.ContentLength == 0 || filecombin.Length < 2)
{
code = "0";
msg = "文件错误!";
}
if (houzhui == "jpg" || houzhui == "jpeg" || houzhui == "png" || houzhui == "gif")
{
//定义本地路径位置
string local = "Upload\\detail_lx_img1";
string filePathName = string.Empty;
string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, local);
var tmpName = Server.MapPath("~/Upload/detail_lx_img1/ ");
var tmp = DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";// file.FileName;
var tmpIndex = 0;
//判断是否存在相同文件名的文件 相同累加1继续判断
//while (System.IO.File.Exists(tmpName + tmp))
//{
// tmp = filecombin[0] + "_" + ++tmpIndex + "." + filecombin[1];
//}
//不带路径的最终文件名
filePathName = tmp;
if (!System.IO.Directory.Exists(localPath))
System.IO.Directory.CreateDirectory(localPath);
string localURL = Path.Combine(local, filePathName);
file.SaveAs(Path.Combine(localPath + "\\temp", filePathName)); //保存图片(文件夹)
string r3 = "..\\" + local + "\\temp" + "\\" + filePathName;
string r2 = "..\\" + local + "\\" + filePathName;
if (MakeThumbnail(r3, r2, 1000, 0) == true)
{
int px1 = 1;
Model.party_member.metting_img m = freeSqlHelper.fsqlDb.Select
if (m != null)
{
int.TryParse(m.px.ToString(), out px1);
px1++;
}
Model.party_member.metting_img m_img = new Model.party_member.metting_img();
m_img.img_src = "../" + localURL.Trim().Replace("\\", "/");
m_img.metting_id = id2;
m_img.px = px1;
var t5 = Common.freeSqlHelper.fsqlDb.Insert(m_img).IgnoreColumns(a => a.ID).ExecuteIdentity();//忽略掉自增列(ID)并返回自增值
if (t5 > 0)
{
code = "1";
src = "../" + localURL.Trim().Replace("\\", "/");
name = Path.GetFileNameWithoutExtension(file.FileName); // 获取文件名不含后缀名
msg = "上传成功";
DeleteImgFile(r3);
urls.Add(src);
Common.Class1.SetLog(userid, get_name1(userid), "对模块图片进行了添加,图片id为" + t5);
}
else
{
code = "0";
msg = "上传图片失败!";
}
}
else
{
code = "0";
msg = "文件转换失败!";
}
}
else
{
code = "0";
msg = "文件必须是图片格式!";
}
}
catch(Exception ex1)
{
}
}
}
}
}
}
catch (Exception ex)
{
code = "0";
msg = ex.Message;
}
return Json(new { code = code, msg = msg, name = name, urls = urls }, JsonRequestBehavior.AllowGet);
}
}
///
/// 生成缩略图
///
/// 源文件地址
/// 目标文件地址
///
///
///
///
public bool MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode = "W")
{
//生成缩略图
try
{
Image originalImage = Image.FromFile(Server.MapPath(originalImagePath));
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(Server.MapPath(thumbnailPath), System.Drawing.Imaging.ImageFormat.Jpeg);
return true;
}
catch (System.Exception e)
{
// throw e;
return false;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
catch (Exception ex)
{
string r10 = ex.Message;
return false;
}
}
private void DeleteImgFile(string fileUrl)
{
//删除指定的文件
string file = System.Web.HttpContext.Current.Server.MapPath(fileUrl);
if (System.IO.File.Exists(file))
{
System.IO.File.Delete(file);
}
}