20023-08-10
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
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;
|
||||
|
||||
@@ -20,7 +23,9 @@ namespace WebAPI.Controllers
|
||||
/// </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))
|
||||
@@ -42,6 +47,10 @@ namespace WebAPI.Controllers
|
||||
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();
|
||||
@@ -169,5 +178,61 @@ namespace WebAPI.Controllers
|
||||
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
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user