[Route("api/uploadFile")]
public ReturnMessage PostFile(string saveDirName)
{
HttpRequest httpRequest = HttpContext.Current.Request;
string rootPath = HttpContext.Current.Server.MapPath("~/");
string saveRootPath = Path.Combine(rootPath, "fileDB");//上传文件的根目录
string realSavePath = Path.Combine(saveRootPath, saveDirName);//要放文件的子目录
ReturnMessage returnMessage = new ReturnMessage();
string v = httpRequest.ServerVariables["REMOTE_ADDR"];
try
{
if (!Directory.Exists(realSavePath))
Directory.CreateDirectory(realSavePath);
if (httpRequest.Files.Count > 0)
{
var docfiles = new List<string>();
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];//上传过来的文件
string savePath = Path.Combine(realSavePath, postedFile.FileName);
postedFile.SaveAs(savePath);
docfiles.Add(savePath);
}
returnMessage.Code = "success";
}
else
{
returnMessage.Code = "failure";
returnMessage.Data = "没有文件";
}
}
catch (Exception ex)
{
returnMessage.Code = "failure";
returnMessage.Data = ex.Message;
}
return returnMessage;
}
标签:httpRequest,文件,asp,string,realSavePath,webapi2.0,returnMessage,上传,net
From: https://www.cnblogs.com/SYF--BLOG/p/17273068.html