我跟着这个:https://plnkr.co/edit/vokjd6?p=preview
并写下我的js:
$scope.AddFormData = new FormData();
$scope.AttachFile = function ($files) {
//$scope.AddFormData.append('chk', 'test');
angular.forEach($files, function (value, key) {
$scope.AddFormData.append('file', value);
});
console.log($scope.AddFormData.entries());
}
$scope.saveStatus = function () {
//FormData
//var AddFormData = new FormData();
//api/JobStatus/save
$scope.AddFormData.append('dStatusDate', JSON.parse(angular.toJson($scope.uStatus.dStatusDate)));
$scope.AddFormData.append('iJobId', JSON.parse(angular.toJson($scope.selJob)));
$scope.AddFormData.append('iStatusId', angular.toJson($scope.uStatus.iStatusId));
$scope.AddFormData.append('vDescription', JSON.parse(angular.toJson($scope.uStatus.vDescription)));
//
console.log($scope.AddFormData.entries());
var request = {
method: 'POST',
url: '/api/save',
data: $scope.AddFormData,
headers: {
'Content-Type': undefined
},
transformRequest: angular.identity
}
$http(request).then(function () {
alert("Posted!");
$scope.init();
}, function () {
alert("Error!");
});
};
如果我不附带文件,它就能完美运行。但是当我添加文件时,我在我的.NET WebAPI中获取该文件,而其他密钥则不存在。在服务器端,FileData
显示内容,FormData
为空。
te POST处理程序的WebAPI代码位于:
//POST: /api/save
[HttpPost]
[Route("api/save")]
public IHttpActionResult PostFile()
{
HttpRequestMessage request = this.Request;
if (!request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
ORG_jobs_status oRG_jobs_status = new ORG_jobs_status();
string root = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/jobstatus");
var provider = new MultipartFormDataStreamProvider(root);
Request.Content.ReadAsMultipartAsync(provider);
try
{
oRG_jobs_status.iJobId = int.Parse(provider.FormData.GetValues("iJobId").FirstOrDefault());
oRG_jobs_status.dStatusDate = DateTime.Parse(provider.FormData.GetValues("dStatusDate").FirstOrDefault());
oRG_jobs_status.iStatusId = int.Parse(provider.FormData.GetValues("iStatusId").FirstOrDefault());
oRG_jobs_status.vDescription = provider.FormData.GetValues("vDescription").FirstOrDefault();
foreach (var file in provider.FileData)
{
oRG_jobs_status.vFileName = file.Headers.ContentDisposition.FileName.Trim('"');
oRG_jobs_status.FileType = file.Headers.ContentType.ToString();
}
//What to store?
//File name to be updated as
//This will go outside
//We save the data, get the autoid and then save the file.
db.ORG_jobs_status.Add(oRG_jobs_status);
db.SaveChanges();
//In future, shall put it with a transaction!
var serverFile = "jst_" + oRG_jobs_status.iJobStatusId.ToString() + "_" + oRG_jobs_status.vFileName;
//File.Move(fileInfo.FullName, Path.Combine(root, guid + "_" + provider.FileData.First().Headers.ContentDisposition.FileName.Replace("\"", "")));
if(!String.IsNullOrEmpty(oRG_jobs_status.vFileName))
File.Move(oRG_jobs_status.vFileName, Path.Combine(root, serverFile));
}
catch (System.Exception e)
{
throw;
//return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
return CreatedAtRoute("DefaultApi", new { controller = oRG_jobs_status, id = oRG_jobs_status.iJobStatusId }, oRG_jobs_status);
}
有人可以指导我解决这个问题吗?