简体   繁体   English

Android调用php脚本下载文件并将其保存在SD卡中

[英]Android call php script to download file and save it in sd-card

I am kind of new to php. 我是php的新手。 I try to make an app in android that downloads a file from the eClass platform. 我尝试在android中制作一个从eClass平台下载文件的应用程序。 I developed this script: 我开发了这个脚本:

    <?php
            $code=$_GET['code'];
            $code = stripslashes($code);
            $code = mysql_real_escape_string($code);

            $filename=$_GET['filename'];
            $filename = stripslashes($filename);
            $filename = mysql_real_escape_string($filename);

            $path = "C:\\xampp\\htdocs\\openeclass-2.5\\courses\\".$code."\\dropbox\\".$filename;
            $fullPath = $path.$_GET['download_file'];

            if ($fd = fopen ($fullPath, "r")) {
                $fsize = filesize($fullPath);
                $path_parts = pathinfo($fullPath);
                $ext = strtolower($path_parts["extension"]);
                switch ($ext) {
                    case "pdf":
                    header("Content-type: application/pdf"); // add here more headers for diff. extensions
                    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // 

            use 'attachment' to force a download
                    break;
                    default;
                    header("Content-type: application/octet-stream");
                    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
                }
                header("Content-length: $fsize");
                header("Cache-control: private"); //use this to open files directly
                while(!feof($fd)) {
                    $buffer = fread($fd, 2048);
                    echo $buffer;
                }
            }
            fclose ($fd);
            exit;
            ?>

which downloads the file when called from browser (in my localhost) and given code and filename as parameters in the url. 当从浏览器(在我的本地主机)中调用该文件时下载文件,并在url中将给定的代码和文件名作为参数。 Now I want to construct a function in my android app that calls this script, gets the file and saves it somewhere in the sd-card 现在,我想在我的Android应用程序中构造一个函数,以调用此脚本,获取文件并将其保存在sd卡中的某个位置

How can I achieve this? 我该如何实现? Also is it possible to be done in the emulator too? 也可以在模拟器中完成吗? Thank you in advance! 先感谢您!

Unfortunately you can't force image download via php. 不幸的是,您不能强制通过php下载图像。 What You need is creating a class that will stream file from web location and save it to sd-card. 您需要创建一个类,该类将从网站流式传输文件并将其保存到sd卡中。

I have simmilar code in my app that does both: 我的应用程序中具有类似的代码,可同时执行以下操作:

try {
    newurl = new URL(iurl); // iurl is a String value
        b = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 
try {
    b.compress(CompressFormat.JPEG, 100, new FileOutputStream(
        sdcard_image)); // sdcard_image is a String value of local filename (including dir)
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }

Hope this would help 希望这会有所帮助

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

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