Boa noite, estou com uma problema. Preciso fazer download de um arquivo no webservice mas para isso é necessário passar dois parametros para que o php faça uma consulta no banco e retorne os bytes do arquivo…
Como eu faço para passar os parametros e logo em seguida fazer o download do arquivo? Não estou conseguindo resolver isso.
Obrigado!
no android:
@Override protected String doInBackground(String... f_url) { int count; try { String cpf = ""; String dataCadastro = "";
URL url = new URL(f_url[0]); // URLConnection conection = url.openConnection(); // conection.connect();
String param="cpf=" + URLEncoder.encode(cpf,"UTF-8") + "&&datacadastro=" + URLEncoder.encode(dataCadastro,"UTF-8");
conn=(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setFixedLengthStreamingMode(param.getBytes().length); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter out = new PrintWriter(conn.getOutputStream()); out.print(param); out.close();
int lenghtOfFile = conn.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192); OutputStream output = new FileOutputStream("/sdcard/documento.pdf");
byte data[] = new byte[1024]; long total = 0;
while ((count = input.read(data)) != -1) { total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile)); output.write(data, 0, count); }
output.flush(); output.close(); input.close(); } catch (Exception e) { Log.e("Error: ", e.getMessage()); } return null; }
o arquivo php:
<?php
include "conexao.php";
$bytes = null; $rows;
if ( isset($_POST["cpf"] ) && isset($_POST["datacadastro"] )){ $cpf = $_POST["cpf"]; $dataCadastro = $_POST["datacadastro"]; $sql = " select conteudo from arquivos where cpf=$cpf and data_cadastro=$dataCadastro"; $result = mysql_query($sql); $num_rows = mysql_num_rows($result); $row = @mysql_fetch_assoc($result); $bytes = $row['conteudo']; header('Content-type: application/pdf'); header("Cache-Control: no-cache"); header("Pragma: no-cache"); header("Content-Disposition: inline;filename='documento.pdf'"); header("Content-length: ".strlen($bytes)); }
echo $bytes;
?>