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























  • You need to upload the file and just receive the uploaded data on Rest API, and if this data is too long you may have to upload by parts,
    – Khaled Lela
    yesterday










  • What is the data type stored on this file path, Is it image, archived or text data.
    – Khaled Lela
    yesterday










  • All mime type it supports .I select file from my mobile which may be media/* or text/* or application/* and I will click save button in my action bar which should take my file from android to rest where I will store my file in specific path of any drivers.
    – Seyed Ali Fathima
    yesterday














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























  • You need to upload the file and just receive the uploaded data on Rest API, and if this data is too long you may have to upload by parts,
    – Khaled Lela
    yesterday










  • What is the data type stored on this file path, Is it image, archived or text data.
    – Khaled Lela
    yesterday










  • All mime type it supports .I select file from my mobile which may be media/* or text/* or application/* and I will click save button in my action bar which should take my file from android to rest where I will store my file in specific path of any drivers.
    – Seyed Ali Fathima
    yesterday












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 yesterday









Khaled Lela

2,52622046




2,52622046










asked Nov 10 at 10:32









Seyed Ali Fathima

84




84











  • You need to upload the file and just receive the uploaded data on Rest API, and if this data is too long you may have to upload by parts,
    – Khaled Lela
    yesterday










  • What is the data type stored on this file path, Is it image, archived or text data.
    – Khaled Lela
    yesterday










  • All mime type it supports .I select file from my mobile which may be media/* or text/* or application/* and I will click save button in my action bar which should take my file from android to rest where I will store my file in specific path of any drivers.
    – Seyed Ali Fathima
    yesterday
















  • You need to upload the file and just receive the uploaded data on Rest API, and if this data is too long you may have to upload by parts,
    – Khaled Lela
    yesterday










  • What is the data type stored on this file path, Is it image, archived or text data.
    – Khaled Lela
    yesterday










  • All mime type it supports .I select file from my mobile which may be media/* or text/* or application/* and I will click save button in my action bar which should take my file from android to rest where I will store my file in specific path of any drivers.
    – Seyed Ali Fathima
    yesterday















You need to upload the file and just receive the uploaded data on Rest API, and if this data is too long you may have to upload by parts,
– Khaled Lela
yesterday




You need to upload the file and just receive the uploaded data on Rest API, and if this data is too long you may have to upload by parts,
– Khaled Lela
yesterday












What is the data type stored on this file path, Is it image, archived or text data.
– Khaled Lela
yesterday




What is the data type stored on this file path, Is it image, archived or text data.
– Khaled Lela
yesterday












All mime type it supports .I select file from my mobile which may be media/* or text/* or application/* and I will click save button in my action bar which should take my file from android to rest where I will store my file in specific path of any drivers.
– Seyed Ali Fathima
yesterday




All mime type it supports .I select file from my mobile which may be media/* or text/* or application/* and I will click save button in my action bar which should take my file from android to rest where I will store my file in specific path of any drivers.
– Seyed Ali Fathima
yesterday












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
    yesterday










  • try is = new FileInputStream(source); int i=0; while((i=is.read())!=-1) System.out.print((char)i); is.close(); catch (IOException e) e.printStackTrace(); ip+"Mobile/rest.svc/Fileacces?stream="+is+"&filename="+fname; how to send file stream from android and get it in my rest function?
    – Seyed Ali Fathima
    yesterday











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
    yesterday










  • try is = new FileInputStream(source); int i=0; while((i=is.read())!=-1) System.out.print((char)i); is.close(); catch (IOException e) e.printStackTrace(); ip+"Mobile/rest.svc/Fileacces?stream="+is+"&filename="+fname; how to send file stream from android and get it in my rest function?
    – Seyed Ali Fathima
    yesterday















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
    yesterday










  • try is = new FileInputStream(source); int i=0; while((i=is.read())!=-1) System.out.print((char)i); is.close(); catch (IOException e) e.printStackTrace(); ip+"Mobile/rest.svc/Fileacces?stream="+is+"&filename="+fname; how to send file stream from android and get it in my rest function?
    – Seyed Ali Fathima
    yesterday













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 Nov 10 at 12:07









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
    yesterday










  • try is = new FileInputStream(source); int i=0; while((i=is.read())!=-1) System.out.print((char)i); is.close(); catch (IOException e) e.printStackTrace(); ip+"Mobile/rest.svc/Fileacces?stream="+is+"&filename="+fname; how to send file stream from android and get it in my rest function?
    – Seyed Ali Fathima
    yesterday

















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










  • try is = new FileInputStream(source); int i=0; while((i=is.read())!=-1) System.out.print((char)i); is.close(); catch (IOException e) e.printStackTrace(); ip+"Mobile/rest.svc/Fileacces?stream="+is+"&filename="+fname; how to send file stream from android and get it in my rest function?
    – Seyed Ali Fathima
    yesterday
















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




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












try is = new FileInputStream(source); int i=0; while((i=is.read())!=-1) System.out.print((char)i); is.close(); catch (IOException e) e.printStackTrace(); ip+"Mobile/rest.svc/Fileacces?stream="+is+"&filename="+fname; how to send file stream from android and get it in my rest function?
– Seyed Ali Fathima
yesterday





try is = new FileInputStream(source); int i=0; while((i=is.read())!=-1) System.out.print((char)i); is.close(); catch (IOException e) e.printStackTrace(); ip+"Mobile/rest.svc/Fileacces?stream="+is+"&filename="+fname; how to send file stream from android and get it in my rest function?
– Seyed Ali Fathima
yesterday


















 

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














































































這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto