2016-10-14 21 views
0

私はアプリケーションで作業していて、イメージをアップロードしています。問題はWCFコードが正しく保存されていないためです。それを無効な画像として保存します。WCFサービスはIonic Appから画像を保存しません

[OperationContract] 
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "upload2/{fileName}")] 
string Upload2(string fileName, Stream fileStream); 

public string Upload2(string fileName, Stream fileStream) 
    { 

     try 
     { 
      FileStream fileToupload = new FileStream(WebConfigurationManager.AppSettings["FilePath"] + fileName, FileMode.Create); 

      byte[] bytearray = new byte[10000]; 
      int bytesRead, totalBytesRead = 0; 
      do 
      { 
       bytesRead = fileStream.Read(bytearray, 0, bytearray.Length); 
       totalBytesRead += bytesRead; 
      } while (bytesRead > 0); 

      fileToupload.Write(bytearray, 0, bytearray.Length); 
      fileToupload.Close(); 
      fileToupload.Dispose(); 
      return "succ"; 
     } 
     catch (Exception ex) 
     { 
      return ex.Message + " - " + ex.InnerException; 
     } 
    } 

コードコントローラAngularJs、

$scope.subirFoto = function() { 
    var options = new FileUploadOptions(); 

    options.fileKey = "post"; 

    options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1); 
    options.mimeType = "image/jpeg"; 
    options.chunkedMode = false; 


    var ft = new FileTransfer(); 

    ft.upload(imageURI, encodeURI("http://192.68.1.182:8085/IServiceTopStore.svc/upload2/"+options.fileName), win, fail, options); 
} 

答えて

0

解決誰かが

[OperationContract] 
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "UploadImage/{fileName}")] 
string UploadImage(string fileName); 

public string UploadImage(string fileName) 
    { 
     try 
     { 
      HttpPostedFile file = HttpContext.Current.Request.Files["post"]; 
      if (file == null) 
       return null; 
      string targetFilePath = WebConfigurationManager.AppSettings["FilePath"] + fileName; 
      file.SaveAs(targetFilePath); 
      return "succ " + file.FileName.ToString(); ; 
     } 
     catch (Exception ex) 
     { 
      return ex.Message + " - " + ex.InnerException; 
     } 
    } 

イオンを必要とする場合には、次のとおりです。

$scope.subirFoto = function() { 
    var options = new FileUploadOptions(); 

    options.fileKey = "post"; 

    options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1); 
    options.mimeType = "image/jpeg"; 
    options.chunkedMode = false; 


    var ft = new FileTransfer(); 

    ft.upload(imageURI, encodeURI("http://192.1.1.1:8085/IServiceTopStore.svc/upload2/"+options.fileName), win, fail, options); 
} 
関連する問題