前台form-data格式 上传文件
public HttpResponseMessage UpLoad()
{
// "请求内容不是表单形式";
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
var json = PostModel("请求内容不是表单形式");
return json;
}
if (HttpContext.Current.Request.Files.Count > 0)
{
Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt");
try
{
var file = HttpContext.Current.Request.Files[0];//获取文件
//获取前端from-data表单形式中的参数
HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
HttpRequestBase request = context.Request;//定义传统request对象
string fname = file.FileName;//获取文件名称
string SavePath = HttpContext.Current.Server.MapPath(string.Format("~/{0}", "合同附件"));
if (!Directory.Exists(SavePath))
{
Directory.CreateDirectory(SavePath);
}
string fullPathUrl = Path.Combine(SavePath, fname);
file.SaveAs(fullPathUrl);
#region 赋值
Appendix app = new Appendix();
app.value = 1;
app.file_url = fullPathUrl;
app.file_name = fname;
app.cid = Convert.ToInt32(request.Form["cid"]);
#endregion
VMAppendix vM = new VMAppendix();
//添加文件
var result = vM.CUDAppendix(app);
var json = PostModel(result);
return json;
}
catch (Exception ex)
{
log.log(" 错误提示:" + ex.ToString());
throw ex;
}
}
return PostModel("无文件!");
}