1.图片上传
Random rd = new Random();
#region 图片上传
[HttpPost]
[Route("UploadPhoto")]
[Description("图片上传")]
public async Task<HttpResponseMessage> UploadPhoto(string type = "")
{
try
{
string guid = Guid.NewGuid().ToString(); ;
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = "/Upload/" + type;
string uploadFolderPath = HostingEnvironment.MapPath(root);
string path = uploadFolderPath + guid;
if (!Directory.Exists(uploadFolderPath))
{
Directory.CreateDirectory(uploadFolderPath);
}
List<string> files = new List<string>();
var provider = new WithExtensionMultipartFormDataStreamProvider(uploadFolderPath, guid);
try
{
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.FileData)
{
files.Add(root + "/" + Path.GetFileName(file.LocalFileName));
}
}
catch (Exception ex)
{
LogHelper.WriteErrorLog("上传图片错误;" + ex.ToString());
return ReturnMessageObj("");
}
return ReturnMessageObj(string.Join(",", files));
}
catch (Exception ex)
{
LogHelper.WriteErrorLog("上传图片错误;" + ex.ToString());
return ReturnMessageObj("");
}
}
[HttpPost]
[Route("UploadFile")]
[Description("文件上传")]
public async Task<HttpResponseMessage> UploadFile(string type = "")
{
try
{
string guid = Guid.NewGuid().ToString(); ;
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = "/Upload/" + type;
string uploadFolderPath = HostingEnvironment.MapPath(root);
string path = uploadFolderPath + guid;
if (!Directory.Exists(uploadFolderPath))
{
Directory.CreateDirectory(uploadFolderPath);
}
List<string> files = new List<string>();
var provider = new WithExtensionMultipartFormDataStreamProvider(uploadFolderPath, guid);
try
{
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.FileData)
{
files.Add(root + "/" + Path.GetFileName(file.LocalFileName));
}
}
catch (Exception ex)
{
LogHelper.WriteErrorLog("上传文件错误;" + ex.ToString());
return ReturnMessageObj("");
}
return ReturnMessageObj(string.Join(",", files));
}
catch (Exception ex)
{
LogHelper.WriteErrorLog("上传图片错误" + ex.ToString());
return ReturnMessageObj("");
}
}
[HttpPost]
[Route("UploadPhoto2")]
[Description("图片上传2号")]
public async Task<HttpResponseMessage> UploadPhotoBatch()
{
try
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = "/Upload/";
string uploadFolderPath = HostingEnvironment.MapPath(root);
if (!Directory.Exists(uploadFolderPath))
{
Directory.CreateDirectory(uploadFolderPath);
}
List<string> files = new List<string>();
var provider = new WithExtensionMultipartFormDataStreamProvider(uploadFolderPath, "");
try
{
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.FileData)
{
files.Add(root + "/" + Path.GetFileName(file.LocalFileName));
}
}
catch (Exception ex)
{
LogHelper.WriteErrorLog("上传图片错误;" + ex.ToString());
return ReturnMessageObj("");
}
return ReturnMessageObj(string.Join(",", files));
}
catch (Exception ex)
{
LogHelper.WriteErrorLog("上传图片错误;" + ex.ToString());
return ReturnMessageObj("");
}
}
public class WithExtensionMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
public string guid { get; set; }
public WithExtensionMultipartFormDataStreamProvider(string rootPath, string guidStr) : base(rootPath)
{
guid = guidStr;
}
public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
{
string fileName = string.IsNullOrEmpty(guid) ? headers.ContentDisposition.Name.Replace("\"", "") : guid;
string extension = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? Path.GetExtension(GetValidFileName(headers.ContentDisposition.FileName)) : "";
string fileName2 = fileName;
int i = 2;
while (File.Exists(HostingEnvironment.MapPath("/Upload/" + fileName2 + extension)))
{
fileName2 = fileName + "_$" + i;
i++;
}
return fileName2 + extension;
}
private string GetValidFileName(string filePath)
{
char[] invalids = System.IO.Path.GetInvalidFileNameChars();
return String.Join("_", filePath.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
}
}