How to send a file to a REST service?











up vote
0
down vote

favorite












I want to get file in rest service from android.In my code,I send filestream from android via url.If I use like this My entire app is not working by saying...



Logcat




Operation Fileacces in contract 'IREST' has a query variable named
stream of type System.IO.FileStream, but type
System.IO.FileStream is not convertible by QueryStringConverter.
 Variables for UriTemplate query values must have types that can
be converted by QueryStringConverter




Here I have post my coding FYR



 public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_PICKER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri content_describer = data.getData();

String src = null;
try {
src = getFilePath(UploadActivity.this, content_describer);
} catch (URISyntaxException e) {
e.printStackTrace();
}
Log.d("src is: ", src);
source = new File(src);
fname = source.getName();

Button text = (Button) findViewById(R.id.txt);
text.setText(fname);


text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileOpen.openFile(UploadActivity.this, source);
} catch (IOException e) {
e.printStackTrace();
}
}
});


}
}


Onclick event in action bar icon:



 @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.mybutton) {
new Save().execute();
return true;
}

return super.onOptionsItemSelected(item);
}


SAVE function asynctask



private class Save extends AsyncTask<String,Void,String> {

protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
try {
fis=new FileInputStream(source);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

ServiceHandler jsonParser = new ServiceHandler();
String fileaccess = ip+"Mobile/rest.svc/Fileacces?stream="+fis+"&filename="+fname;
Log.d("Fileaccess","fileaccess"+fileaccess);
String result = jsonParser.makeServiceCall(fileaccess, ServiceHandler.GET);
Log.i("fileaccess", "result: > " + result);
return null;
}


@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
System.out.println(s);

}

}


My rest function in vb.net



 Public Function fileacces(ByVal stream As FileStream, ByVal filename As String) As String Implements IREST.fileacces
Dim result = ""

Try

Dim path = "D:XXXYYYECM SourceArchiveEZECMSettingsMonitor" + filename
Dim fileStream = New FileStream(path, FileMode.Create, FileAccess.Write)
stream.CopyTo(fileStream)
fileStream.Dispose()
result = "file uploaded"


Catch ex As Exception

End Try
Return result
End Function









share|improve this question




























    up vote
    0
    down vote

    favorite












    I want to get file in rest service from android.In my code,I send filestream from android via url.If I use like this My entire app is not working by saying...



    Logcat




    Operation Fileacces in contract 'IREST' has a query variable named
    stream of type System.IO.FileStream, but type
    System.IO.FileStream is not convertible by QueryStringConverter.
     Variables for UriTemplate query values must have types that can
    be converted by QueryStringConverter




    Here I have post my coding FYR



     public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == FILE_PICKER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
    Uri content_describer = data.getData();

    String src = null;
    try {
    src = getFilePath(UploadActivity.this, content_describer);
    } catch (URISyntaxException e) {
    e.printStackTrace();
    }
    Log.d("src is: ", src);
    source = new File(src);
    fname = source.getName();

    Button text = (Button) findViewById(R.id.txt);
    text.setText(fname);


    text.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    try {
    FileOpen.openFile(UploadActivity.this, source);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    });


    }
    }


    Onclick event in action bar icon:



     @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.mybutton) {
    new Save().execute();
    return true;
    }

    return super.onOptionsItemSelected(item);
    }


    SAVE function asynctask



    private class Save extends AsyncTask<String,Void,String> {

    protected void onPreExecute() {
    super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
    try {
    fis=new FileInputStream(source);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

    ServiceHandler jsonParser = new ServiceHandler();
    String fileaccess = ip+"Mobile/rest.svc/Fileacces?stream="+fis+"&filename="+fname;
    Log.d("Fileaccess","fileaccess"+fileaccess);
    String result = jsonParser.makeServiceCall(fileaccess, ServiceHandler.GET);
    Log.i("fileaccess", "result: > " + result);
    return null;
    }


    @Override
    protected void onPostExecute(String s) {
    super.onPostExecute(s);
    System.out.println(s);

    }

    }


    My rest function in vb.net



     Public Function fileacces(ByVal stream As FileStream, ByVal filename As String) As String Implements IREST.fileacces
    Dim result = ""

    Try

    Dim path = "D:XXXYYYECM SourceArchiveEZECMSettingsMonitor" + filename
    Dim fileStream = New FileStream(path, FileMode.Create, FileAccess.Write)
    stream.CopyTo(fileStream)
    fileStream.Dispose()
    result = "file uploaded"


    Catch ex As Exception

    End Try
    Return result
    End Function









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to get file in rest service from android.In my code,I send filestream from android via url.If I use like this My entire app is not working by saying...



      Logcat




      Operation Fileacces in contract 'IREST' has a query variable named
      stream of type System.IO.FileStream, but type
      System.IO.FileStream is not convertible by QueryStringConverter.
       Variables for UriTemplate query values must have types that can
      be converted by QueryStringConverter




      Here I have post my coding FYR



       public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == FILE_PICKER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
      Uri content_describer = data.getData();

      String src = null;
      try {
      src = getFilePath(UploadActivity.this, content_describer);
      } catch (URISyntaxException e) {
      e.printStackTrace();
      }
      Log.d("src is: ", src);
      source = new File(src);
      fname = source.getName();

      Button text = (Button) findViewById(R.id.txt);
      text.setText(fname);


      text.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      try {
      FileOpen.openFile(UploadActivity.this, source);
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      });


      }
      }


      Onclick event in action bar icon:



       @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.mybutton) {
      new Save().execute();
      return true;
      }

      return super.onOptionsItemSelected(item);
      }


      SAVE function asynctask



      private class Save extends AsyncTask<String,Void,String> {

      protected void onPreExecute() {
      super.onPreExecute();
      }

      @Override
      protected String doInBackground(String... params) {
      try {
      fis=new FileInputStream(source);
      } catch (FileNotFoundException e) {
      e.printStackTrace();
      }

      ServiceHandler jsonParser = new ServiceHandler();
      String fileaccess = ip+"Mobile/rest.svc/Fileacces?stream="+fis+"&filename="+fname;
      Log.d("Fileaccess","fileaccess"+fileaccess);
      String result = jsonParser.makeServiceCall(fileaccess, ServiceHandler.GET);
      Log.i("fileaccess", "result: > " + result);
      return null;
      }


      @Override
      protected void onPostExecute(String s) {
      super.onPostExecute(s);
      System.out.println(s);

      }

      }


      My rest function in vb.net



       Public Function fileacces(ByVal stream As FileStream, ByVal filename As String) As String Implements IREST.fileacces
      Dim result = ""

      Try

      Dim path = "D:XXXYYYECM SourceArchiveEZECMSettingsMonitor" + filename
      Dim fileStream = New FileStream(path, FileMode.Create, FileAccess.Write)
      stream.CopyTo(fileStream)
      fileStream.Dispose()
      result = "file uploaded"


      Catch ex As Exception

      End Try
      Return result
      End Function









      share|improve this question















      I want to get file in rest service from android.In my code,I send filestream from android via url.If I use like this My entire app is not working by saying...



      Logcat




      Operation Fileacces in contract 'IREST' has a query variable named
      stream of type System.IO.FileStream, but type
      System.IO.FileStream is not convertible by QueryStringConverter.
       Variables for UriTemplate query values must have types that can
      be converted by QueryStringConverter




      Here I have post my coding FYR



       public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == FILE_PICKER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
      Uri content_describer = data.getData();

      String src = null;
      try {
      src = getFilePath(UploadActivity.this, content_describer);
      } catch (URISyntaxException e) {
      e.printStackTrace();
      }
      Log.d("src is: ", src);
      source = new File(src);
      fname = source.getName();

      Button text = (Button) findViewById(R.id.txt);
      text.setText(fname);


      text.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      try {
      FileOpen.openFile(UploadActivity.this, source);
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      });


      }
      }


      Onclick event in action bar icon:



       @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.mybutton) {
      new Save().execute();
      return true;
      }

      return super.onOptionsItemSelected(item);
      }


      SAVE function asynctask



      private class Save extends AsyncTask<String,Void,String> {

      protected void onPreExecute() {
      super.onPreExecute();
      }

      @Override
      protected String doInBackground(String... params) {
      try {
      fis=new FileInputStream(source);
      } catch (FileNotFoundException e) {
      e.printStackTrace();
      }

      ServiceHandler jsonParser = new ServiceHandler();
      String fileaccess = ip+"Mobile/rest.svc/Fileacces?stream="+fis+"&filename="+fname;
      Log.d("Fileaccess","fileaccess"+fileaccess);
      String result = jsonParser.makeServiceCall(fileaccess, ServiceHandler.GET);
      Log.i("fileaccess", "result: > " + result);
      return null;
      }


      @Override
      protected void onPostExecute(String s) {
      super.onPostExecute(s);
      System.out.println(s);

      }

      }


      My rest function in vb.net



       Public Function fileacces(ByVal stream As FileStream, ByVal filename As String) As String Implements IREST.fileacces
      Dim result = ""

      Try

      Dim path = "D:XXXYYYECM SourceArchiveEZECMSettingsMonitor" + filename
      Dim fileStream = New FileStream(path, FileMode.Create, FileAccess.Write)
      stream.CopyTo(fileStream)
      fileStream.Dispose()
      result = "file uploaded"


      Catch ex As Exception

      End Try
      Return result
      End Function






      android rest file






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 14 hours ago









      Khaled Lela

      2,52622046




      2,52622046










      asked yesterday









      Seyed Ali Fathima

      84




      84
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote














          kindly use base 64




          like this



          public void uploadFile(final String selectedFilePath) {
          try {


          FileInputStream inputStream = new FileInputStream(selectedFilePath);
          byte byteArray = IOUtils.toByteArray(inputStream);

          final String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
          getActivity().runOnUiThread(new Runnable() {
          @Override
          public void run() {
          // tvFileName.setText(encoded);
          Log.e("File Base 64",encoded);
          filebas64=encoded;
          }
          });


          } catch (FileNotFoundException e) {
          e.printStackTrace();
          Log.e("Error",e.toString());

          } catch (IOException e) {
          e.printStackTrace();
          Log.e("Error",e.toString());

          }

          }





          share|improve this answer








          New contributor




          Vignesh Waran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Sorry I am not getting.Please tell me where to use the above function and explain me how it will work?
            – Seyed Ali Fathima
            35 mins ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238072%2fhow-to-send-a-file-to-a-rest-service%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote














          kindly use base 64




          like this



          public void uploadFile(final String selectedFilePath) {
          try {


          FileInputStream inputStream = new FileInputStream(selectedFilePath);
          byte byteArray = IOUtils.toByteArray(inputStream);

          final String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
          getActivity().runOnUiThread(new Runnable() {
          @Override
          public void run() {
          // tvFileName.setText(encoded);
          Log.e("File Base 64",encoded);
          filebas64=encoded;
          }
          });


          } catch (FileNotFoundException e) {
          e.printStackTrace();
          Log.e("Error",e.toString());

          } catch (IOException e) {
          e.printStackTrace();
          Log.e("Error",e.toString());

          }

          }





          share|improve this answer








          New contributor




          Vignesh Waran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Sorry I am not getting.Please tell me where to use the above function and explain me how it will work?
            – Seyed Ali Fathima
            35 mins ago















          up vote
          0
          down vote














          kindly use base 64




          like this



          public void uploadFile(final String selectedFilePath) {
          try {


          FileInputStream inputStream = new FileInputStream(selectedFilePath);
          byte byteArray = IOUtils.toByteArray(inputStream);

          final String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
          getActivity().runOnUiThread(new Runnable() {
          @Override
          public void run() {
          // tvFileName.setText(encoded);
          Log.e("File Base 64",encoded);
          filebas64=encoded;
          }
          });


          } catch (FileNotFoundException e) {
          e.printStackTrace();
          Log.e("Error",e.toString());

          } catch (IOException e) {
          e.printStackTrace();
          Log.e("Error",e.toString());

          }

          }





          share|improve this answer








          New contributor




          Vignesh Waran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Sorry I am not getting.Please tell me where to use the above function and explain me how it will work?
            – Seyed Ali Fathima
            35 mins ago













          up vote
          0
          down vote










          up vote
          0
          down vote










          kindly use base 64




          like this



          public void uploadFile(final String selectedFilePath) {
          try {


          FileInputStream inputStream = new FileInputStream(selectedFilePath);
          byte byteArray = IOUtils.toByteArray(inputStream);

          final String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
          getActivity().runOnUiThread(new Runnable() {
          @Override
          public void run() {
          // tvFileName.setText(encoded);
          Log.e("File Base 64",encoded);
          filebas64=encoded;
          }
          });


          } catch (FileNotFoundException e) {
          e.printStackTrace();
          Log.e("Error",e.toString());

          } catch (IOException e) {
          e.printStackTrace();
          Log.e("Error",e.toString());

          }

          }





          share|improve this answer








          New contributor




          Vignesh Waran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.










          kindly use base 64




          like this



          public void uploadFile(final String selectedFilePath) {
          try {


          FileInputStream inputStream = new FileInputStream(selectedFilePath);
          byte byteArray = IOUtils.toByteArray(inputStream);

          final String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
          getActivity().runOnUiThread(new Runnable() {
          @Override
          public void run() {
          // tvFileName.setText(encoded);
          Log.e("File Base 64",encoded);
          filebas64=encoded;
          }
          });


          } catch (FileNotFoundException e) {
          e.printStackTrace();
          Log.e("Error",e.toString());

          } catch (IOException e) {
          e.printStackTrace();
          Log.e("Error",e.toString());

          }

          }






          share|improve this answer








          New contributor




          Vignesh Waran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Vignesh Waran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered yesterday









          Vignesh Waran

          11




          11




          New contributor




          Vignesh Waran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Vignesh Waran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Vignesh Waran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • Sorry I am not getting.Please tell me where to use the above function and explain me how it will work?
            – Seyed Ali Fathima
            35 mins ago


















          • Sorry I am not getting.Please tell me where to use the above function and explain me how it will work?
            – Seyed Ali Fathima
            35 mins ago
















          Sorry I am not getting.Please tell me where to use the above function and explain me how it will work?
          – Seyed Ali Fathima
          35 mins ago




          Sorry I am not getting.Please tell me where to use the above function and explain me how it will work?
          – Seyed Ali Fathima
          35 mins ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238072%2fhow-to-send-a-file-to-a-rest-service%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues