http headers - How to send file name with NanoHttpd Response -


i've achieved file transfer on local network using nanohttpd. however, i'm unable send file name in nanohttpd response. received files have default name this: localhost_8080. tried attach file name in response header using content-disposition, file transfer failed together. doing wrong? here implementation:

private class webserver extends nanohttpd {     string mime_type;     file file;      public webserver() {         super(port);     }      @override     public response serve(string uri, method method,             map<string, string> header, map<string, string> parameters,             map<string, string> files) {         try {             file=new file(filetostream);             fis = new fileinputstream(file);             bis = new bufferedinputstream(fis);             mime_type= urlconnection.guesscontenttypefromname(file.getname());         } catch (ioexception ioe) {             log.w("httpd", ioe.tostring());         }         nanohttpd.response res=new nanohttpd.response(status.ok, mime_type, bis);         res.addheader("content-disposition: attachment; filename=", file.getname());         return res;     } } 

thanks help!

you need specify response, mime type, , stream of bytes sent. after add header file name of file since http method. here sample code solves problem

@override public response serve(string uri, method method,     map<string, string> header, map<string, string> parameters,     map<string, string> files) {      fileinputstream fis = null;     try {         fis = new fileinputstream(filename);     } catch (filenotfoundexception e) {         e.printstacktrace();     }      nanohttpd.response res = new nanohttpd.response(response.status.ok, "application/vnd.android.package-archive", fis);     res.addheader("content-disposition", "attachment; filename=\""+filename+"\"");      return res;  } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -