【发布时间】:2015-03-24 10:11:27
【问题描述】:
我正在尝试使用 https://github.com/danialfarid/angular-file-upload 将图像上传到我在不同域中的 webAPI。
我的 HTML:
<input
type="file"
ng-file-select="onFileSelect($files)"
ng-model="imgData"
accept="image/*"
ng-click="test()" >
我的控制器:
app.controller('userController', [ 'removed', '$upload',
function (removed, $upload) {
$scope.onFileSelect = function ($files) {
console.log('onFileSelect'); // --------- THIS METHOD DOES NOT FIRE
$http.post(serviceBase + 'api/person/image', data, {
withCredentials: true,
transformRequest: angular.identity
}).success('ok').error('fail');
}
// tried different things from all the resources found online:
$scope.test = function () {
// THIS WORKS but how to get the file??
// successfull call to controller method but unable to retrieve image file inside controller
$http.post(serviceBase + 'api/person/image', data).then(function (response) {
return response;
});
// unable to call controller method ('Resourse not found', CORS issue?)
$scope.upload = $upload.upload({
url: 'person/image',
headers: { 'Authorization': 'bearer placeHolderText' },
file: $scope.imgData,
) };
// unable to call controller method ('Resourse not found', CORS issue?)
$http.post(serviceBase + 'api/person/image', data, {
withCredentials: true,
transformRequest: angular.identity
}).success('ok').error('fail');}}
API 控制器方法:
[HttpPost()]
[ActionName("image")]
[ResponseType(typeof(JObject))]
public async Task<IHttpActionResult> Postimage(HttpPostedFileBase file)
{
**更新:启用 CORS 详细信息... (Microsoft.Owin.Cors) **
我的 Startup.cs:
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
public void ConfigureOAuth(IAppBuilder app)
{
//use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseExternalSignInCookie(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() {
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = new SimpleAuthorizationServerProvider(),
RefreshTokenProvider = new SimpleRefreshTokenProvider()
};
这很可能是 CORS 问题,因为我可以使用 $HTTP.Post 发布到该控制器方法。我在服务器上启用了 CORS。我已经阅读和尝试了两天的东西,但我遇到了困难,非常感谢任何建议/建议。
更新 2:
又是一天的研究/试错:
我可以发这个:
$scope.upload = $upload.upload({
url: 'http://localhost:26264/api/person/image', //upload.php script, node.js route, or servlet url
file: $scope.imgData,
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
但现在我从提琴手那里得到“415(不支持的媒体类型)”.. 详细信息:
"此资源不支持请求实体的媒体类型 'multipart/form-data'。","exceptionMessage":"没有 MediaTypeFormatter 可用于从媒体类型为 'multipart' 的内容中读取类型为 'HttpPostedFileBase' 的对象/form-data'。”
【问题讨论】:
-
你是如何启用 cors 的?是否安装了nuget包
Microsoft.AspNet.WebApi.Cors,在webapi配置中添加config.EnableCors();,然后在控制器中添加[EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")]? -
我正在使用 Microsoft.Owin.Cors 所以我没有 [EnableCors(origins: "mywebclient.azurewebsites.net", headers: "", methods: "") ] 可供我使用。我被困住了,请帮忙!!
标签: javascript angularjs asp.net-web-api file-upload