How to force download a file from server in iphone browser

String filePath = "http://images.all-free-download.com/images
        /graphiclarge/baby_child_girl_215819.jpg";

         The above String is my download path.

          URL url = new URL(filePath);
        HttpURLConnection httpConn = (HttpURLConnection)   
        url.openConnection();
        InputStream inStream = httpConn.getInputStream();

        // if you want to use a relative path to context root:
        @SuppressWarnings("unused")
        String relativePath = getServletContext().getRealPath("");

        // obtains ServletContext
        ServletContext context = getServletContext();

        // gets MIME type of the file
        String mimeType = context.getMimeType(filePath);
        if (mimeType == null) {        
            // set to binary type if MIME mapping not found
            mimeType = "application/octet-stream";
        }

        // modifies response
        response.setContentType(mimeType);
        response.setContentLength((int) httpConn.getContentLength());

        String fileName = filePath.substring(filePath.lastIndexOf("/") +
         1,
                filePath.length());

        // forces download
        //String headerKey = "Content-Disposition";
        //String headerValue = String.format("attachment; 
         filename=\"%s\"", fileName);
        response.setHeader("Content-Transfer-Encoding", "binary");
        response.setHeader("Content-Disposition","attachment; 
          filename=\""+ fileName);
        //response.setHeader(headerKey, headerValue);

        // obtains response output stream
        OutputStream outStream = response.getOutputStream();

        byte[] buffer = new byte[4096];
        int bytesRead = -1;

        while ((bytesRead = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }

        inStream.close();
        outStream.close(); 

      

The above code works fine for android device in browser. But its not working in iphone.

After clicking the download button, the image is displayed directly. Don't ask anythink.

I want to save image to gallery in iphone.
Can anyone help me. Is it possible for iphone.

+3


source to share





All Articles