1

Phonegap ionic framework을 사용하여 Android App을 구현하려고합니다. 내 요구 사항은 갤러리에서 원격 서버로 이미지를 업로드하는 것입니다. 이를 위해 서버에 이미지를 업로드하기 위해 ASP.NETREST API을 구현했습니다. (업로드 할 내 URL : http://XXXX.net/api/Upload). 내 REST API에서는 UploadController:WebController을 구현했습니다. 나는 아래 보이는 Post() 메소드를 가지고있다.FileTransfer.Upload ASP.NET REST API를 사용하여 이미지 업로드

서버 코드 :

public string Post() 
{ 
    HttpPostedFile MyFile = HttpContext.Current.Request.Files["recFile"]; 
    if (MyFile == null) 
     return null; 
    return Utils.UploadToServer(file); 
} 

내 Android 코드는 다음과 같습니다

$scope.UploadPictureEx = function() 
{ 
    var options = new FileUploadOptions(); 
    options.fileKey="recFile"; 
    options.fileName=$scope.emplyeedata.ImageURI.substr($scope.emplyeedata.ImageURI.lastIndexOf('/')+1); 
    options.mimeType="image/jpeg"; 
    var params = new Object(); 
    params.value1 = "test"; 
    params.value2 = "param"; 
    options.params = params; 
    options.chunkedMode = false; 
    options.httpMethod = "POST"; 
    options.headers = { 
      Connection: "close" 
    }; 
    var ft = new FileTransfer(); 
    ft.upload($scope.emplyeedata.ImageURI, "http://XXXX.net/api/Upload/", win, fail, options); 
} 

그게 전부에요. 나는 항상 오류 메시지가 Error code = 3이라고 말하는 것을 실행합니다. 아무도이 일을 어떻게 처리하는지 알려주실 수 있습니까? 내 웹 API 구현 방식이 맞습니까? 나는 어떤 일을 잘못하고 있니? 감사합니다.

+0

업데이트를 :

public static class WebApiConfig { public static void Register(HttpConfiguration config) { // New code config.EnableCors(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } 

및 컨트롤러는 미세 입자의 수 ​​사용하려는 것을 제어에

? –

답변

0

웹 API에서 cors을 사용 설정 했습니까?

Install-Package Microsoft.AspNet.WebApi.Cors 

및 웹 API를 설정에서

이에

[EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")] 
public class TestController : ApiController 
{ 
    // Controller methods not shown... 
} 
관련 문제