Files
COMCMS/COMCMS.Web/Areas/AdminCP/Controllers/UploadController.cs
T
2018-04-15 23:30:15 +08:00

766 lines
35 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using COMCMS.Common;
using COMCMS.Core;
using XCode;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;
using COMCMS.Web.Models;
using Newtonsoft.Json;
using System.IO;
using COMCMS.Core.Models;
using System.DrawingCore;
using System.Text;
using Microsoft.AspNetCore.Http.Internal;
namespace COMCMS.Web.Areas.AdminCP.Controllers
{
/// <summary>
/// 后台上传文件帮助类
/// </summary>
[Area("AdminCP")]
public class UploadController : AdminBaseController
{
private readonly SystemSetting _attachsetting;
private IHostingEnvironment _env;
private AttachConfigEntity attach;
public UploadController( IHostingEnvironment env, IOptions<SystemSetting> attachsetting)
{
attach = Config.GetSystemConfig().AttachConfigEntity;
_env = env;
_attachsetting = attachsetting.Value;
}
#region CKEditor 上传图片
/// <summary>
/// ckeditor 上传图片
/// </summary>
/// <returns></returns>
public async Task<IActionResult> CKUploadImage()
{
string callback = Request.Query["CKEditorFuncNum"];//要求返回值
var upload = Request.Form.Files[0];
string tpl = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(\"{1}\", \"{0}\", \"{2}\");</script>";
if (upload == null)
return Content(string.Format(tpl, "", callback, "请选择一张图片!"), "text/html");
//判断是否是图片类型
List<string> imgtypelist = new List<string> { "image/pjpeg", "image/png", "image/x-png", "image/gif", "image/bmp", "image/jpeg" };
if (imgtypelist.FindIndex(x => x == upload.ContentType) == -1)
{
return Content(string.Format(tpl, "", callback, "请上传一张图片!"), "text/html");
}
var data = Request.Form.Files["upload"];
string filepath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\images\\";
string thumbsFilePath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\_thumbs\\images\\";
//根据附件配置,设置上传图片目录
string imgPath = DateTime.Now.Year.ToString();//默认按年
switch (attach.SaveType)
{
case 1://按月份
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}";
break;
case 2:
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}\\{DateTime.Now.ToString("dd")}";
break;
}
filepath += imgPath;//存放路径
thumbsFilePath += imgPath;//缩略图路径
string sFileNameNoExt = Utils.GetFileNameWithoutExtension(upload.FileName);//文件名字,不带扩展名
string sFullExtension = Utils.GetFileExtName(upload.FileName);//扩展名
//图片名字
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(upload.FileName);
switch (attach.IsRandomFileName)
{
case 0://不随机
imgname = upload.FileName;
//判断是否存在
if (System.IO.File.Exists(Path.Combine(filepath, imgname)))
{
imgname = sFileNameNoExt + "(1)" + sFullExtension;
}
break;
case 1://随机字符串
imgname = Utils.GetShortGUId() + sFullExtension;
break;
case 2://时间
imgname = Utils.GetOrderNum() + sFullExtension;
break;
}
string fullpath = Path.Combine(filepath, imgname);//图片
string fullThumbPath = Path.Combine(thumbsFilePath, imgname);//缩略图
try
{
//判断路径
if (!Directory.Exists(filepath))
Directory.CreateDirectory(filepath);
//缩略图路径
if (!Directory.Exists(thumbsFilePath))
Directory.CreateDirectory(thumbsFilePath);
if (data != null)
{
await Task.Run(() =>
{
using (FileStream fs = new FileStream(fullpath, FileMode.Create))
{
data.CopyTo(fs);
}
});
//生成缩略图
if (attach.IsCreateThum == 1)
{
ThumbnailHelper.MakeThumbnailImage(fullpath, fullThumbPath, attach.ThumMaxWidth, attach.ThumMaxHeight, ThumbnailHelper.CutMode.Cut);
}
//添加水印
if (attach.IsWaterMark == 1 && !string.IsNullOrEmpty(attach.WaterMarkImg))
{
string watermarkimg = _env.WebRootPath + attach.WaterMarkImg.Replace("/", "\\");
if (System.IO.File.Exists(watermarkimg))
{
//先复制一张图片出来
string copyfullpath = fullpath.Replace(sFullExtension, "_copy" + sFullExtension);
System.IO.File.Copy(fullpath, copyfullpath);
Image waterpic = new Bitmap(watermarkimg);
Image srcPic = new Bitmap(copyfullpath);
if (waterpic.Width < srcPic.Width && waterpic.Height < srcPic.Height)
{
waterpic.Dispose();
//srcPic.Dispose();
try
{
WatermarkHelper.AddImageSignPic(srcPic, fullpath, watermarkimg, attach.WaterMarkPlace, attach.WaterMarkQty, attach.WaterMarkDiaphaneity);
srcPic.Dispose();
System.IO.File.Delete(copyfullpath);
}
catch
{
if (System.IO.File.Exists(copyfullpath))
{
System.IO.File.Delete(copyfullpath);
}
}
}
else
{
waterpic.Dispose();
srcPic.Dispose();
}
}
}
}
}
catch (Exception ex)
{
return Content(string.Format(tpl, "", callback, "图片上传失败:" + ex.Message), "text/html");
}
return Content(string.Format(tpl, $"/{attach.AttachPatch}/images/{imgPath.Replace("\\", "/")}/" + imgname, callback, ""), "text/html");
}
#endregion
#region CKEditor 上传文件
public async Task<IActionResult> CKUploadFile()
{
string callback = Request.Query["CKEditorFuncNum"];//要求返回值
var upload = Request.Form.Files[0];
string tpl = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(\"{1}\", \"{0}\", \"{2}\");</script>";
if (upload == null)
return Content(string.Format(tpl, "", callback, "请选择一个文件!"), "text/html");
string sFileNameNoExt = Utils.GetFileNameWithoutExtension(upload.FileName);//文件名字,不带扩展名
string sFullExtension = Utils.GetFileExtName(upload.FileName);//扩展名
if (string.IsNullOrEmpty(sFullExtension))
{
return Content(string.Format(tpl, "", callback, $"错误的文件类型!"), "text/html");
}
//判断是否是允许文件扩展名
string sAllowedExtensions = _attachsetting.FileAllowedExtensions;
List<string> listAllowedExtensions = new List<string>();
string[] arrAllowedExtensions = sAllowedExtensions.Split(new string[] { "," }, StringSplitOptions.None);
if (arrAllowedExtensions != null && arrAllowedExtensions.Length > 0)
{
foreach (var s in arrAllowedExtensions)
{
listAllowedExtensions.Add(s);
}
}
if (listAllowedExtensions.Find(x => x == sFullExtension.ToLower().Replace(".", "")) == null)
{
return Content(string.Format(tpl, "", callback, $"{sFullExtension}的文件类型,不允许上传!"), "text/html");
}
//判断是否是图片,如果是图片,后面需要生成缩略图并可能的话,就加上水印
bool isImage = false;
//判断是否是图片类型
List<string> imgtypelist = new List<string> { "image/pjpeg", "image/png", "image/x-png", "image/gif", "image/bmp" };
if (imgtypelist.FindIndex(x => x == upload.ContentType) >= 0)
{
isImage = true;
}
var data = Request.Form.Files["upload"];
string filepath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\files\\";
string thumbsFilePath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\_thumbs\\files\\";
//根据附件配置,设置上传图片目录
string imgPath = DateTime.Now.Year.ToString();//默认按年
switch (attach.SaveType)
{
case 1://按月份
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}";
break;
case 2:
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}\\{DateTime.Now.ToString("dd")}";
break;
}
//文件名字
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(upload.FileName);
switch (attach.IsRandomFileName)
{
case 0://不随机
imgname = upload.FileName;
//判断是否存在
if (System.IO.File.Exists(Path.Combine(filepath, imgname)))
{
imgname = sFileNameNoExt + "(1)" + sFullExtension;
}
break;
case 1://随机字符串
imgname = Utils.GetShortGUId() + sFullExtension;
break;
case 2://时间
imgname = Utils.GetOrderNum() + sFullExtension;
break;
}
string fullpath = Path.Combine(filepath, imgname);//图片
string fullThumbPath = Path.Combine(thumbsFilePath, imgname);//缩略图,可能的话
try
{
//判断路径
if (!Directory.Exists(filepath))
Directory.CreateDirectory(filepath);
//缩略图路径
if (isImage && !Directory.Exists(thumbsFilePath))
Directory.CreateDirectory(thumbsFilePath);
if (data != null)
{
await Task.Run(() =>
{
using (FileStream fs = new FileStream(fullpath, FileMode.Create))
{
data.CopyTo(fs);
}
});
//生成缩略图
if (isImage && attach.IsCreateThum == 1)
{
ThumbnailHelper.MakeThumbnailImage(fullpath, fullThumbPath, attach.ThumMaxWidth, attach.ThumMaxHeight, ThumbnailHelper.CutMode.Cut);
}
//添加水印
if (isImage && attach.IsWaterMark == 1 && !string.IsNullOrEmpty(attach.WaterMarkImg))
{
string watermarkimg = _env.WebRootPath + attach.WaterMarkImg.Replace("/", "\\");
if (System.IO.File.Exists(watermarkimg))
{
//先复制一张图片出来
string copyfullpath = fullpath.Replace(sFullExtension, "_copy" + sFullExtension);
System.IO.File.Copy(fullpath, copyfullpath);
Image waterpic = new Bitmap(watermarkimg);
Image srcPic = new Bitmap(copyfullpath);
if (waterpic.Width < srcPic.Width && waterpic.Height < srcPic.Height)
{
waterpic.Dispose();
//srcPic.Dispose();
try
{
WatermarkHelper.AddImageSignPic(srcPic, fullpath, watermarkimg, attach.WaterMarkPlace, attach.WaterMarkQty, attach.WaterMarkDiaphaneity);
srcPic.Dispose();
System.IO.File.Delete(copyfullpath);
}
catch
{
if (System.IO.File.Exists(copyfullpath))
{
System.IO.File.Delete(copyfullpath);
}
}
}
else
{
waterpic.Dispose();
srcPic.Dispose();
}
}
}
}
}
catch (Exception ex)
{
return Content(string.Format(tpl, "", callback, "文件上传失败:" + ex.Message), "text/html");
}
return Content(string.Format(tpl, $"/{attach.AttachPatch}/fales/{imgPath.Replace("\\", "/")}/" + imgname, callback, ""), "text/html");
}
#endregion
#region CKEditor 上传多媒体
#endregion
#region Webuploader 上传图片
//public IActionResult WUUploadImage()
//{
// string isbinary = Request.Query["isbinary"];
// switch (isbinary)
// {
// case "1"://二进制上传
// return DoWebuploaderImageByBinary();
// default://默认上传
// return DoWebuploaderImage();
// }
//}
#region 二进制上传图片
/// <summary>
/// 二进制上传图片(未实现)
/// </summary>
/// <returns></returns>
public IActionResult DoWebuploaderImageByBinary()
{
var fileinfo = "";
var msg = "上传失败!";
var status = 0;
var name = "";
var path = "";
var thumb = "";
var size = "";
var ext = "";
try
{
string upname = Request.Query["name"];
//判断是否是允许文件扩展名
string sAllowedExtensions = _attachsetting.ImageAllowedExtensions;
List<string> listAllowedExtensions = new List<string>();
string[] arrAllowedExtensions = sAllowedExtensions.Split(new string[] { "," }, StringSplitOptions.None);
string sFileNameNoExt = Utils.GetFileNameWithoutExtension(upname);//文件名字,不带扩展名
string sFullExtension = Utils.GetFileExtName(upname);//扩展名
if (arrAllowedExtensions != null && arrAllowedExtensions.Length > 0)
{
foreach (var s in arrAllowedExtensions)
{
listAllowedExtensions.Add(s);
}
}
if (listAllowedExtensions.Find(x => x == sFullExtension.ToLower().Replace(".", "")) == null)
{
return Json(new { status = status, msg = $"{sFullExtension}的文件类型,不允许上传,请上传一张图片!", name = name, path = path, thumb = thumb, size = size, ext = ext });
}
var req = HttpContext.Request;
var f = Request.Form.Files[0];
Request.EnableRewind();
Image image = null;
using (StreamReader reader = new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
{
var bytes = ConvertToBinary(reader.BaseStream);
image = ReturnPic(bytes);
size = bytes.Length.ToString();
}
string filepath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\images\\";
string thumbsFilePath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\_thumbs\\images\\";
//根据附件配置,设置上传图片目录
string imgPath = DateTime.Now.Year.ToString();//默认按年
switch (attach.SaveType)
{
case 1://按月份
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}";
break;
case 2:
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}\\{DateTime.Now.ToString("dd")}";
break;
}
filepath += imgPath;//存放路径
thumbsFilePath += imgPath;//缩略图路径
//图片名字
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(upname);
switch (attach.IsRandomFileName)
{
case 0://不随机
imgname = upname;
//判断是否存在
if (System.IO.File.Exists(Path.Combine(filepath, imgname)))
{
imgname = sFileNameNoExt + "(1)" + sFullExtension;
}
break;
case 1://随机字符串
imgname = Utils.GetShortGUId() + sFullExtension;
break;
case 2://时间
imgname = Utils.GetOrderNum() + sFullExtension;
break;
}
string fullpath = Path.Combine(filepath, imgname);//图片
string fullThumbPath = Path.Combine(thumbsFilePath, imgname);//缩略图
//判断路径
if (!Directory.Exists(filepath))
Directory.CreateDirectory(filepath);
//缩略图路径
if (!Directory.Exists(thumbsFilePath))
Directory.CreateDirectory(thumbsFilePath);
//保存图片
image.Save(filepath);
//生成缩略图
if (attach.IsCreateThum == 1)
{
ThumbnailHelper.MakeThumbnailImage(fullpath, fullThumbPath, attach.ThumMaxWidth, attach.ThumMaxHeight, ThumbnailHelper.CutMode.Cut);
}
//添加水印
if (attach.IsWaterMark == 1 && !string.IsNullOrEmpty(attach.WaterMarkImg))
{
string watermarkimg = _env.WebRootPath + attach.WaterMarkImg.Replace("/", "\\");
if (System.IO.File.Exists(watermarkimg))
{
//先复制一张图片出来
string copyfullpath = fullpath.Replace(sFullExtension, "_copy" + sFullExtension);
System.IO.File.Copy(fullpath, copyfullpath);
Image waterpic = new Bitmap(watermarkimg);
Image srcPic = new Bitmap(copyfullpath);
if (waterpic.Width < srcPic.Width && waterpic.Height < srcPic.Height)
{
waterpic.Dispose();
//srcPic.Dispose();
try
{
WatermarkHelper.AddImageSignPic(srcPic, fullpath, watermarkimg, attach.WaterMarkPlace, attach.WaterMarkQty, attach.WaterMarkDiaphaneity);
srcPic.Dispose();
System.IO.File.Delete(copyfullpath);
}
catch
{
if (System.IO.File.Exists(copyfullpath))
{
System.IO.File.Delete(copyfullpath);
}
}
}
else
{
waterpic.Dispose();
srcPic.Dispose();
}
}
}
image.Dispose();
status = 1;
name = imgname;
path = $"/{attach.AttachPatch}/images/{imgPath.Replace("\\", "/")}/" + imgname;
thumb = $"/{attach.AttachPatch}/_thumbs/images/{imgPath.Replace("\\", "/")}/" + imgname;
ext = sFullExtension;
fileinfo = $"/{attach.AttachPatch}/images/{imgPath.Replace("\\", "/")}/" + imgname;
msg = "上传成功!";
}
catch (Exception ex)
{
msg = ex.Message;
}
return Json(new { status = status, msg = msg, name = name, path = path, thumb = thumb, size = size, ext = ext });
}
#endregion
/// <summary>
/// 上传图片
/// </summary>
/// <returns></returns>
public async Task<IActionResult> DoWebuploaderImage()
{
var msg = "上传失败!";
var status = 0;
var name = "";
var path = "";
var thumb = "";
var size = "";
var ext = "";
var data = Request.Form.Files[0];
if (data != null)
{
//判断是否是图片类型
List<string> imgtypelist = new List<string> { "image/pjpeg", "image/png", "image/x-png", "image/gif", "image/bmp", "image/jpeg" };
if (imgtypelist.FindIndex(x => x == data.ContentType) == -1)
{
msg = "只能上传一张图片格式文件!";
return Content(JsonConvert.SerializeObject(new { status = status, msg = msg, name = name, path = path, thumb = thumb, size = size, ext = ext }), "text/plain");
}
string filepath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\images\\";
string thumbsFilePath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\_thumbs\\images\\";
//根据附件配置,设置上传图片目录
string imgPath = DateTime.Now.Year.ToString();//默认按年
switch (attach.SaveType)
{
case 1://按月份
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}";
break;
case 2:
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}\\{DateTime.Now.ToString("dd")}";
break;
}
filepath += imgPath;//存放路径
thumbsFilePath += imgPath;//缩略图路径
string sFileNameNoExt = Utils.GetFileNameWithoutExtension(data.FileName);//文件名字,不带扩展名
string sFullExtension = Utils.GetFileExtName(data.FileName);//扩展名
//图片名字
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(data.FileName);
switch (attach.IsRandomFileName)
{
case 0://不随机
imgname = data.FileName;
//判断是否存在
if (System.IO.File.Exists(Path.Combine(filepath, imgname)))
{
imgname = sFileNameNoExt + "(1)" + sFullExtension;
}
break;
case 1://随机字符串
imgname = Utils.GetShortGUId() + sFullExtension;
break;
case 2://时间
imgname = Utils.GetOrderNum() + sFullExtension;
break;
}
string fullpath = Path.Combine(filepath, imgname);//图片
string fullThumbPath = Path.Combine(thumbsFilePath, imgname);//缩略图
try
{
//判断路径
if (!Directory.Exists(filepath))
Directory.CreateDirectory(filepath);
//缩略图路径
if (!Directory.Exists(thumbsFilePath))
Directory.CreateDirectory(thumbsFilePath);
if (data != null)
{
await Task.Run(() =>
{
using (FileStream fs = new FileStream(fullpath, FileMode.Create))
{
data.CopyTo(fs);
}
});
//生成缩略图
if (attach.IsCreateThum == 1)
{
ThumbnailHelper.MakeThumbnailImage(fullpath, fullThumbPath, attach.ThumMaxWidth, attach.ThumMaxHeight, ThumbnailHelper.CutMode.Cut);
}
string notwater = Request.Query["notwater"];//是否强制不加水印
if (string.IsNullOrEmpty(notwater) || notwater != "1") notwater = "0";
//添加水印
if (notwater == "0" && attach.IsWaterMark == 1 && !string.IsNullOrEmpty(attach.WaterMarkImg))
{
string watermarkimg = _env.WebRootPath + attach.WaterMarkImg.Replace("/", "\\");
if (System.IO.File.Exists(watermarkimg))
{
//先复制一张图片出来
string copyfullpath = fullpath.Replace(sFullExtension, "_copy" + sFullExtension);
System.IO.File.Copy(fullpath, copyfullpath);
Image waterpic = new Bitmap(watermarkimg);
Image srcPic = new Bitmap(copyfullpath);
if (waterpic.Width < srcPic.Width && waterpic.Height < srcPic.Height)
{
waterpic.Dispose();
//srcPic.Dispose();
try
{
WatermarkHelper.AddImageSignPic(srcPic, fullpath, watermarkimg, attach.WaterMarkPlace, attach.WaterMarkQty, attach.WaterMarkDiaphaneity);
srcPic.Dispose();
System.IO.File.Delete(copyfullpath);
}
catch
{
if (System.IO.File.Exists(copyfullpath))
{
System.IO.File.Delete(copyfullpath);
}
}
}
else
{
waterpic.Dispose();
srcPic.Dispose();
}
}
}
}
status = 1;
name = imgname;
path = $"/{attach.AttachPatch}/images/{imgPath.Replace("\\", "/")}/" + imgname;
thumb = $"/{attach.AttachPatch}/_thumbs/images/{imgPath.Replace("\\", "/")}/" + imgname;
ext = sFullExtension;
msg = "上传成功!";
}
catch (Exception ex)
{
msg = "图片上传失败:" + ex.Message;
}
}
else
{
msg = "请上传一张图片";
}
return Content(JsonConvert.SerializeObject(new { status = status, msg = msg, name = name, path = path, thumb = thumb, size = size, ext = ext }), "text/plain");
}
#endregion
#region Webuploader 上传文件
/// <summary>
/// 上传图片
/// </summary>
/// <returns></returns>
public async Task<IActionResult> DoWebuploaderFile()
{
var msg = "上传失败!";
var status = 0;
var name = "";
var path = "";
var thumb = "";
var size = "";
var ext = "";
var data = Request.Form.Files[0];
if (data != null)
{
string filepath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\files\\";
string thumbsFilePath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\_thumbs\\files\\";
//根据附件配置,设置上传图片目录
string imgPath = DateTime.Now.Year.ToString();//默认按年
switch (attach.SaveType)
{
case 1://按月份
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}";
break;
case 2:
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}\\{DateTime.Now.ToString("dd")}";
break;
}
filepath += imgPath;//存放路径
thumbsFilePath += imgPath;//缩略图路径
string sFileNameNoExt = Utils.GetFileNameWithoutExtension(data.FileName);//文件名字,不带扩展名
string sFullExtension = Utils.GetFileExtName(data.FileName);//扩展名
//判断是否是允许文件扩展名
string sAllowedExtensions = _attachsetting.FileAllowedExtensions;
List<string> listAllowedExtensions = new List<string>();
string[] arrAllowedExtensions = sAllowedExtensions.Split(new string[] { "," }, StringSplitOptions.None);
if (arrAllowedExtensions != null && arrAllowedExtensions.Length > 0)
{
foreach (var s in arrAllowedExtensions)
{
listAllowedExtensions.Add(s);
}
}
if (listAllowedExtensions.Find(x => x == sFullExtension.ToLower().Replace(".", "")) == null)
{
msg = $"{sFullExtension}的文件类型,不允许上传!";
return Content(JsonConvert.SerializeObject(new { status = status, msg = msg, name = name, path = path, thumb = thumb, size = size, ext = ext }), "text/plain");
}
//图片名字
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(data.FileName);
switch (attach.IsRandomFileName)
{
case 0://不随机
imgname = data.FileName;
//判断是否存在
if (System.IO.File.Exists(Path.Combine(filepath, imgname)))
{
imgname = sFileNameNoExt + "(1)" + sFullExtension;
}
break;
case 1://随机字符串
imgname = Utils.GetShortGUId() + sFullExtension;
break;
case 2://时间
imgname = Utils.GetOrderNum() + sFullExtension;
break;
}
string fullpath = Path.Combine(filepath, imgname);//图片
string fullThumbPath = Path.Combine(thumbsFilePath, imgname);//缩略图
try
{
//判断路径
if (!Directory.Exists(filepath))
Directory.CreateDirectory(filepath);
//缩略图路径
if (!Directory.Exists(thumbsFilePath))
Directory.CreateDirectory(thumbsFilePath);
if (data != null)
{
await Task.Run(() =>
{
using (FileStream fs = new FileStream(fullpath, FileMode.Create))
{
data.CopyTo(fs);
}
});
}
status = 1;
name = imgname;
path = $"/{attach.AttachPatch}/files/{imgPath.Replace("\\", "/")}/" + imgname;
thumb = $"/{attach.AttachPatch}/_thumbs/files/{imgPath.Replace("\\", "/")}/" + imgname;
ext = sFullExtension;
msg = "上传成功!";
}
catch (Exception ex)
{
msg = "图片上传失败:" + ex.Message;
}
}
else
{
msg = "请上传一个文件";
}
return Content(JsonConvert.SerializeObject(new { status = status, msg = msg, name = name, path = path, thumb = thumb, size = size, ext = ext }), "text/plain");
}
#endregion
#region 帮助方法
public byte[] ConvertToBinary(Stream stream)
{
// 保存为byte[]
byte[] byData = new byte[stream.Length];
stream.Read(byData, 0, byData.Length);
stream.Dispose();
return byData;
}
/// <summary>
/// 二进制转图片
/// </summary>
/// <param name="streamByte"></param>
/// <returns></returns>
public Image ReturnPic(byte[] streamByte)
{
MemoryStream ms = new MemoryStream(streamByte);
Image img = Image.FromStream(ms);
return img;
}
#endregion
}
}