2014-11-13 5 views
1

내가이 가능한 API가 : 뒤에서 코드에서PhoneGap에서 Asp.Net 웹 API로 이미지를 업로드하는 방법은 무엇입니까?

[HttpPost] 
[CorsEnabled] 
[ActionName("upstream")] 
public DTO.Callback UploadPhoto(Stream data) 
{ 
    var m = new Logic.Components.User(); 
    return m.UploadPhoto(data, Common.UserValues().Email); 
} 

[HttpPost] 
[CorsEnabled] 
[ActionName("upbyte")] 
public DTO.Callback UploadPhoto(byte[] data) 
{ 
    var m = new Logic.Components.User(); 
    return m.UploadPhoto(data, Common.UserValues().Email); 
} 

[HttpPost] 
[CorsEnabled] 
[ActionName("upfile")] 
public DTO.Callback UploadPhoto(HttpPostedFileBase data) 
{ 
    var m = new Logic.Components.User(); 
    return m.UploadPhoto(data, Common.UserValues().Email); 
} 

[HttpPost] 
[CorsEnabled] 
[ActionName("up")] 
public DTO.Callback UploadPhoto(DTO.UserPhoto data) 
{ 
    var m = new Logic.Components.User(); 
    return m.UploadPhoto(data, Common.UserValues().Email); 
} 

UserPhoto 클래스

public class UserPhoto 
{ 
    public string Base64 { get; set; } 
    public byte[] Data { get; set; } 
} 

을, 나는 변환하거나 각 요청 데이터의 해당 바이트 []를 얻을하려고합니다.
올바른 이미지 바이트를 얻으려면 좋은 방법입니다.

카메라 열립니다 함수 :

takePicture: function (success, error) { 
    var s = function (data) { 
      navigator.camera.cleanup(); 
      (success || angular.noop)(data); 
     }, 
     e = function (data) { 
      navigator.camera.cleanup(); 
      (error || angular.noop)(data); 
     }; 

    navigator.camera.getPicture(s, e, 
     { 
      quality: 100, 
      destinationType: Camera.DestinationType.FILE_URI, 
      sourceType: Camera.PictureSourceType.CAMERA, 
      encodingType: Camera.EncodingType.PNG, 
      correctOrientation: true 
     } 
    ); 
} 

내 첫 번째 시도는 base64로 문자열로 이미지를 변환하고 API '까지'를 사용하는 것입니다 내 폰갭 응용 프로그램에서

, 나는이 코드가 .
낮은 quality의 값이 50보다 크지 않아도 정상적으로 작동합니다. 그러나 이미지는 거의 인식 할 수 없게됩니다. 그래서 quality100으로 설정했습니다. 그리고 나서 새로운 문제가 생기면 전화가 끊깁니다 ...

그래서 FileTransfer을 사용하려고했습니다.

fileTransfer: function (filePath, serverUri, mimeType, params, success, error) { 
    var 
     u = $coreData.user.getSession() 
     ; 

    var options = new FileUploadOptions(); 
    options.fileKey = 'file'; 
    options.mimeType = mimeType; 
    options.params = params; 
    options.chunkedMode = true; 
    options.headers = { 
     Connection: 'close', 
     Device: m.createDeviceHeader(), 
     'Authentication-Token': (u && u.SessionKey), 
     'Content-Type': 'application/json' 
    }; 

    var ft = new FileTransfer(); 
    ft.upload(filePath, encodeURI(serverUri), success, error, options); 
} 

샘플 사용 : 다음은 코드입니다

uploadFile: function (path) { 
    var def = $q.defer(); 
    $coreUtility 
     .fileTransfer(path, $coreAPI.user.getUrl('upfile'), 'image/png', null, 
     function (success) { 


      def.resolve(m.callback(true, UPLOAD_PHOTO_SUCCESS, success)); 
     }, 
     function (error) { 


      def.reject(m.callback(false, UPLOAD_PHOTO_FAIL, error)); 
     }); 
    return def.promise; 
} 

하지만 파일을 업로드 할 수 없습니다, 난 항상 미디어 타입 포맷터 때로는 null 참조 예외를 지원하지 얻을. 나는 완전히 생각에서 벗어났다.

답변

2

알겠습니다. 지금 받으십시오. 같은 문제에 대한 다른 고투를 위해, 여기에 해결책이 있습니다.

[HttpPost] 
    [CorsEnabled] 
    [ActionName("upfile")] 
    public DTO.Callback UploadPhoto() 
    { 
     var m = new Logic.Components.User(); 
     return m.UploadPhoto(HttpContext.Current.Request, Common.UserValues().Email); 
    } 

일부 논리 :

public DTO.Callback UploadPhoto(HttpRequest req, string email) 
    { 
     if (req.Files.Count > 0) 
     { 
      var file = req.Files[0]; 
      var m = new Logic.Components.User(); 
      return m.UploadPhoto(file.InputStream, email); 
     } 

     return new DTO.Callback { Message = "Fail to upload. Make sure that you are uploading an image file." }; 
    } 

솔루션에 대한 몇 가지 설명 :

첫 번째 부분은 당신의 API이고, 두 번째 부분은 백엔드 코드입니다.

ASP.Net MVC Web APIPhoneGap에서 이미지 스트림을 전달하려면, 당신은 단지 폰갭에서 Stream를 얻을 수 HttpContext.Current.Request을 사용해야합니다.

+0

해결 방법을 알려주시겠습니까? 감사합니다 – clintgh

+0

내 편집 참조 ...... – fiberOptics

관련 문제