我正在尝试使用angulards
中的formdata对象创建上传文件<?php
$sql=mysql_query("SELECT * FROM category")or die(mysql_error());
$result=mysql_fetch_array($sql);
for($i=0;$i<$result;$i++)
{
$cat_id=mysql_result($get_cat,$i,'category_id');
$cat=mysql_result($get_cat,$i,'category_name');
$category_icon=mysql_result($get_cat,$i,'category_icon');
echo $cat_id;
echo $cat . '<img src="data:image/jpeg;base64,' . base64_encode($num_cat['category_icon']) . '" width="290" height="290">' ;
}
?>
希望在mvc 6中使用webapi2编写c#控制器代码代码
答案 0 :(得分:4)
var fd = new FormData();
fd.append('file', $scope.teamLogo);
fd.append('aa', 'a');
$http.post('http://localhost:51815/api/upload/', fd, {
transformRequest: saangular.identity,
headers: { 'Content-Type': undefined }
}).then(function (teamList) { });
控制器中的
public async Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
// Trace.WriteLine(file.Headers.ContentDisposition.FileName);
//Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
在js文件中使用此代码