public class FileAPIController : BaseController
{
private readonly string prefix = "thumb_";
private readonly Token token;
private readonly ServiceImpl.Config.AppConfig config;
/// <summary>
/// 上传图片
/// </summary>
/// <param name="token"></param>
/// <param name="config"></param>
public FileAPIController(Token token, ServiceImpl.Config.AppConfig config)
{
this.config = config;
this.token = token;
if (token == null)
{
throw new ApiException(HttpStatusCode.Unauthorized, "用户登录已过期!");
}
}
#region 上传文件
/// <summary>
/// 上传文件
/// </summary>
/// <param name="subpath">文件子目录</param>
/// <param name="thumbnail">缩略图大小,格式:width*height,为空不生成缩略图</param>
/// <returns></returns>
[HttpPost]
[Route("v1/file/{subpath}")]
public async Task<IEnumerable<string>> Upload(string subpath = null, string thumbnail = null)
{
if (token == null || token.EntCode == null)
{
throw new ApiException(HttpStatusCode.Unauthorized, "用户登录已过期!");
}
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
List<string> files = new List<string>();
try
{
MultipartMemoryStreamProvider provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
#region 文件目录
string root = GetRoot(subpath, string.Empty);
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
#endregion
foreach (HttpContent file in provider.Contents)
{
string filename = file.Headers.ContentDisposition.FileName.Trim('\"');
byte[] buffer = await file.ReadAsByteArrayAsync();
string savename = Gui