简体   繁体   English

使用AsyncTask从PHP站点下载文件,而没有任何“打开/保存”对话框

[英]Using AsyncTask to download file from PHP site without any Open/Save dialog box

My android app would have a link on it to a server that is hosting a file. 我的Android应用程序上会带有指向托管文件的服务器的链接。 I would like my app to download this file from the server in the background, so that the user can keep the app in focus and continue doing what he wants to do. 我希望我的应用在后台从服务器下载此文件,以便用户可以使应用保持焦点并继续执行他想做的事情。

I assume that AsyncTask can be used to do this in background. 我假设可以使用AsyncTask在后台执行此操作。 The link in the app is to a php page, which has the following code - 应用程序中的链接是指向php页面的,该页面具有以下代码-

header("Content-type: application/xls");
header("Content-Disposition: attachment; filename=\myFile.xls\"");
readfile("myFile.xls"); 

From what I understand, the readfile() will get data from the server, and then write it into its buffer. 据我了解,readfile()将从服务器获取数据,然后将其写入其缓冲区。 And the AsyncTask would read from this buffer and store it into the location in the phone as specified. 然后AsyncTask将从此缓冲区读取并将其存储到指定位置的电话中。

My question - 1. Do let me know if my approach is correct (what I have described above) 2. I assume that the header("Content-Disposition: ..) will result in a OPEN/SAVE Dialog box (as in the case of normal desktop browser dialog box). Will similar dialog box be displayed when the link is called by an android app as well? If yes, is there someway to not show this dialog box and instead just download, so that the user need not bother where the file is getting stored in his phone? 我的问题-1.让我知道我的方法是否正确(上面已经描述过)2.我假设header(“ Content-Disposition:..)将导致OPEN / SAVE对话框(如(如果是普通的桌面浏览器对话框)。android应用程序调用链接时也会显示类似的对话框吗?如果是,是否有某种方式不显示此对话框而只是下载,因此用户不需要麻烦文件存储在手机中的位置了吗?

Thanks! 谢谢!

Async task is let's you do this in the background. 异步任务是让您在后台执行此操作。 The PHP page returns the contents of the xml-file. PHP页面返回xml文件的内容。 Using HttpGet to get the contents, and then saving them to a location your choosing. 使用HttpGet获取内容,然后将其保存到您选择的位置。

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet ("http://urltopage.com/page");

HttpResponse response = httpclient.execute(httpget);
sb = new StringBuffer("");
in = new BufferedReader(new InputStreamReader(response
                        .getEntity().getContent()));

String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
    sb.append(line + NL);

}
in.close();

Then you can do what you want with the content now in text format. 然后,您可以立即以文本格式处理内容。 The stringbuilder is not a must, but that way you get a nice clean string. stringbuilder不是必须的,但是那样您就可以得到一个干净的字符串。 I think you could just write it to a file straight away instead of building a string. 我认为您可以直接将其写入文件中,而不用构建字符串。

If you want to put it straight to a file: 如果要直接放入文件中:

FileOutputStream fos = new FileOutputStream(newFile("\path to file"));

byte[] buff = new byte[4096];
int len; 
while((len = in.read(buff)) > 0) {
      fos.write(buff, 0, len);
}


fos.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM