CNCEC_SUBQHSE_WUHUAN/SGGL/WebAPI/Controllers/FileUploadController.cs

239 lines
9.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BLL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
namespace WebAPI.Controllers
{
/// <summary>
/// 附件上传
/// </summary>
public class FileUploadController : ApiController
{
#region
/// <summary>
/// 附件上传
/// </summary>
/// <returns></returns>
public IHttpActionResult Post()
{// 定义允许上传的文件类型列表
List<string> allowExtensions = BLL.DropListService.allowExtensions;
HttpFileCollection files = HttpContext.Current.Request.Files;
string typeName = HttpContext.Current.Request["typeName"];
if (string.IsNullOrEmpty(typeName))
{
typeName = "WebApi";
}
string reUrl = string.Empty;
if (files != null && files.Count > 0)
{
string folderUrl = "FileUpLoad/" + typeName + "/" + DateTime.Now.ToString("yyyy-MM") + "/";
string localRoot = ConfigurationManager.AppSettings["localRoot"] + folderUrl; //物理路径
if (!Directory.Exists(localRoot))
{
Directory.CreateDirectory(localRoot);
}
foreach (string key in files.AllKeys)
{
string rootUrl = string.Empty;
string fileName = string.Empty;
string extensionstr = string.Empty;
HttpPostedFile file = files[key];//file.ContentLength文件长度
if (!allowExtensions.Contains(Path.GetExtension(file.FileName)))
{
return BadRequest($"Invalid file extension: {file.FileName}");
}
if (!string.IsNullOrEmpty(file.FileName))
{
extensionstr = Path.GetExtension(file.FileName).ToLower();
fileName = SQLHelper.GetNewID() + extensionstr;
rootUrl = localRoot + fileName;
file.SaveAs(localRoot + fileName);
}
if (extensionstr==".jpg" || extensionstr == ".gif" || extensionstr == ".bmp" || extensionstr == ".png")
{
string TakePicDateTime = string.Empty;
System.Drawing.Image image = System.Drawing.Image.FromStream(file.InputStream, true, false);
Encoding ascii = Encoding.ASCII;
//遍历图像文件元数据,检索所有属性
foreach (System.Drawing.Imaging.PropertyItem p in image.PropertyItems)
{
//如果是PropertyTagDateTime则返回该属性所对应的值
if (p.Id == 0x0132)
{
TakePicDateTime = ascii.GetString(p.Value);
}
}
TakePicDateTime = string.IsNullOrEmpty(TakePicDateTime) ? string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) : TakePicDateTime;
if (!string.IsNullOrEmpty(TakePicDateTime))
{
////获取元数据中的拍照日期时间,以字符串形式保存
//TakePicDateTime = GetTakePicDateTime(pi);
//分析字符串分别保存拍照日期和时间的标准格式
var SpaceLocation = TakePicDateTime.IndexOf(" ");
var dt = TakePicDateTime.Substring(0, SpaceLocation);
dt = dt.Replace(":", "-");
var tm = TakePicDateTime.Substring(SpaceLocation + 1, TakePicDateTime.Length - SpaceLocation - 2);
TakePicDateTime = dt + " " + tm;
//由列表中的文件创建内存位图对象
var Pic = new Bitmap(rootUrl);
//由位图对象创建Graphics对象的实例
var g = Graphics.FromImage(Pic);
Font ft = new Font("宋体", 20, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));//定义字体
//在Graphics表面绘制数码照片的日期/时间戳
g.DrawString(TakePicDateTime, ft, Brushes.Gold, 0, Pic.Height - 100);
// - 50);
string newRoot = localRoot + "newfile/";
if (!Directory.Exists(newRoot))
{
Directory.CreateDirectory(newRoot);
}
//将添加日期/时间戳后的图像进行保存
Pic.Save(newRoot + fileName);
fileName = "newfile/" + fileName;
//释放内存位图对象
Pic.Dispose();
}
}
if (!string.IsNullOrEmpty(fileName))
{
if (string.IsNullOrEmpty(reUrl))
{
reUrl += folderUrl + fileName;
}
else
{
reUrl += "," + folderUrl + fileName;
}
}
}
}
return Ok(reUrl);
}
#endregion
#region
/// <summary>
/// 保存附件信息
/// </summary>
/// <param name="toDoItem">附件</param>
[HttpPost]
public Model.ResponeData SaveAttachUrl([FromBody] Model.ToDoItem toDoItem)
{
var responeData = new Model.ResponeData();
try
{
APIUpLoadFileService.SaveAttachUrl(toDoItem);
}
catch (Exception ex)
{
responeData.code = 0;
responeData.message = ex.Message;
}
return responeData;
}
#endregion
#region
/// <summary>
/// 附件上传
/// </summary>
/// <returns></returns>
public IHttpActionResult PostSmall()
{
HttpFileCollection files = HttpContext.Current.Request.Files;
string typeName = HttpContext.Current.Request["typeName"];
if (string.IsNullOrEmpty(typeName))
{
typeName = "WebApi";
}
string reUrl = string.Empty;
if (files != null && files.Count > 0)
{
string folderUrl = "FileUpLoad/" + typeName + "/" + DateTime.Now.ToString("yyyy-MM") + "/";
string localRoot = ConfigurationManager.AppSettings["localRoot"] + folderUrl; //物理路径
if (!Directory.Exists(localRoot))
{
Directory.CreateDirectory(localRoot);
}
foreach (string key in files.AllKeys)
{
HttpPostedFile file = files[key];//file.ContentLength文件长度
reUrl = UpLoadImageService.UpLoadImage(file, folderUrl, folderUrl + "Small/", 80, 30);
}
}
return Ok(reUrl);
}
#endregion
#region 线
/// <summary>
/// 在线编辑回调保存文件
/// </summary>
/// <param name="toDoItem"></param>
/// <returns></returns>
[HttpPost]
public async Task<Model.ResponeData> DownloadDocument([FromBody] Model.SaveOnlineFileItem toDoItem)
{
var responeData = new Model.ResponeData();
HttpClient _httpClient = new HttpClient();
try
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
// 发送 GET 请求以获取文件内容
var response = await _httpClient.GetAsync(toDoItem.UrlStr);
response.EnsureSuccessStatusCode();
string savePath = "";
if (!string.IsNullOrEmpty(toDoItem.PCUrl))
{
savePath = ConfigurationManager.AppSettings["localRoot"] + toDoItem.PCUrl;
}
if (savePath != null)
{
// 将文件内容保存到本地
using (var fileStream = File.Create(savePath))
{
await response.Content.CopyToAsync(fileStream);
}
responeData.code = 1;
responeData.data = "文件已成功下载并保存到本地";
}
else
{
responeData.code = 0;
responeData.message = "保存路径为空";
}
}
catch (Exception ex)
{
responeData.code = 0;
responeData.message = $"下载文件时出现错误:{ex.Message}";
}
return responeData;
}
#endregion
}
}