跳至主要内容

博文

目前显示的是标签为“file”的博文

javascript upload file with progress bar (xml request)

<!DOCTYPE html> <html> <head> <title>my title</title> <meta name="viewport" content="width=device-width initial-scale=1 maximum-scale=1"> <script type="text/javascript"> function up() {         if (document.getElementById("f").value == "") {             document.getElementById("result").innerHTML = "请选择文件";         }         else {             var fileObj = document.getElementById("f").files[0];             //创建xhr             var xhr = new XMLHttpRequest();             var url = "uploadFile";             //FormData对象             var fd = new FormData();             fd.append("path", "D:\\");    //上传路径     ...

android open file provider type

add a FileProvider tag in AndroidManifest.xml under tag. <? xml version = "1.0" encoding = "utf-8" ?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... <application ... <provider android:name = "android.support.v4.content.FileProvider" android:authorities = "${applicationId}.provider" android:exported = "false" android:grantUriPermissions = "true" > <meta-data android:name = "android.support.FILE_PROVIDER_PATHS" android:resource = "@xml/provider_paths" /> </provider> </application> </manifest> then create a provider_paths.xml file in xml folder under res folder. Folder may be needed to create if it doesn't exist. The content of the file is shown below. It describes that we would like to ...

java copy file (android)

private static void copyFileUsingStream ( File source , File dest ) throws IOException { InputStream is = null ; OutputStream os = null ; try { is = new FileInputStream ( source ); os = new FileOutputStream ( dest ); byte [] buffer = new byte [ 1024 ]; int length ; while (( length = is . read ( buffer )) > 0 ) { os . write ( buffer , 0 , length ); } } finally { is . close (); os . close (); } }

android add a file to apk

The  assets/  folder in apk is intended to store any extra user files in any formats. They will be included to apk automatically on compilation stage and can be accessed from the app using  getAssets()  function. For example: final InputStream is = getResources (). getAssets (). open ( "some_file.xml" )

go handle upload multiple File

package main import ( f "fmt" "html/template" "io" "net/http" "os" ) func main() { http.HandleFunc("/", home) http.HandleFunc("/uploadfiles", uploadfiles) http.ListenAndServe(":8080", nil) } func home(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles("index.html") t.Execute(w, nil) } func uploadfiles(w http.ResponseWriter, r *http.Request) { r.ParseMultipartForm(1 << 30) fhs := r.MultipartForm.File["uploadFile"] for _, v := range fhs { file, err := v.Open() if err != nil { f.Println(err) return } mf, err := os.OpenFile("./"+v.Filename, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { f.Println(err) return } defer mf.Close() io.Copy(mf, file) } f.Fprintf(w, "ok") } //index.html <!DOCTYPE html> <html> <head> <title>upload</title...

go ServeFile

package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "sublime_text.tar.bz2")) w.Header().Add("Content-Type", "application/octet-stream") http.ServeFile(w, r, "/home/steven/sublime_text.tar.bz2") }) err := http.ListenAndServe(":8090", nil) if err != nil { fmt.Println(err) } }

go fileserver

package main import ( "fmt" "net/http" ) func main() { http.Handle("/s/", http.StripPrefix("/s/",     http.FileServer(http.Dir("/home/steven/Downloads")))) err := http.ListenAndServe(":8090", nil) if err != nil { fmt.Println(err) } }

go copy file

func Copy(src, dst string) error { in, err := os.Open(src) if err != nil { return err } defer in.Close() out, err := os.Create(dst) if err != nil { return err } defer out.Close() _, err = io.Copy(out, in) cerr := out.Close() if err != nil { return err } return cerr }

android write external file

Permission : writeExternalStorage public boolean isStoragePermissionGranted() { if (Build.VERSION. SDK_INT >= 23 ) { if (checkSelfPermission(Manifest.permission. WRITE_EXTERNAL_STORAGE ) == PackageManager. PERMISSION_GRANTED && checkSelfPermission(Manifest.permission. READ_EXTERNAL_STORAGE ) == PackageManager. PERMISSION_GRANTED ) { Log. v ( TAG , "Permission is granted" ); return true ; } else { Log. v ( TAG , "Permission is revoked" ); ActivityCompat. requestPermissions ( this , new String[]{Manifest.permission. WRITE_EXTERNAL_STORAGE }, 1 ); ActivityCompat. requestPermissions ( this , new String[]{Manifest.permission. READ_EXTERNAL_STORAGE }, 1 ); return false ; } } else { //permission is automatically granted on sdk<23 upon installation Log. v ( TAG , "Permission is gra...

android file chooser

private void showFileChooser (){ Intent intent= new Intent(Intent. ACTION_GET_CONTENT ) ; intent.setType( "*/*" ) ; intent.addCategory(Intent. CATEGORY_OPENABLE ) ; try { startActivityForResult(Intent. createChooser (intent , "Select a File to share" ) , 0 ) ; } catch (android.content.ActivityNotFoundException ex){ Toast. makeText ( this, "Please install a File Manager" , Toast. LENGTH_SHORT ).show() ; } } @Override protected void onActivityResult ( int requestCode , int resultCode , Intent data) { switch (requestCode){ case 0 : if (resultCode== RESULT_OK ){ Uri uri=data.getData() ; Log. d ( "spy" , "##FileSharer: uri=" +uri.toString()) ; try { String path= getPath ( this, uri) ; Log. d ( "spy" , "## File path=" +path) ; } catch (URISyntaxException e) ...

服务器下载文件Http头的设置

网站提供下载服务时经常需要实现一个强制下载功能(即强制弹出下载对话框),并且文件名保持和用户之前上传时相同。 效果如下图:  Content-Disposition 使用 HTTP Header 的 Content-Disposition: attachment 可以实现下载时强制弹出下载对话框。 由于HTTP协议规定,通信内容使用US ASCII编码,就是只能使用英文字符集。若要使用其他字符集,必须根据RFC3986使用百分号将字符串编码。 Content-Disposition : attachment; filename=filename.ext Content-Disposition : attachment; filename*=charset'lang'encoded-filename.ext 如果不进行编码会出现,用户保存文件文件名会是乱码。如下图: 不过关于Content-Disposition的 RFC6266规范 是2011年6月才纳入HTTP标准。 浏览器方面我测试了主流浏览器,Firefox 、 Chrome 、 Opera 、 Safari ,都支持新标准规定的 filename ,不出意料,万恶的IE并不支持这个规范。不过我还是很吃惊,IE10竟然也不支持filename 。 按照规范输出Content-Disposition的 PHP 代码如下: if ( strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], "MSIE" ) > 0 ) { header ( 'Content-Disposition: attachment; filename="' . rawurlencode ( $originfile ) . '"' ); } else { header( 'Content-Disposition: attachment; filename*=UTF-8\' \ '' . rawurlencode ( $originfile ) ); } 注意编码时使用 rawurlencode...

Java中读取字符文件类FileReader

File Read&Write Android

Read: FileReader fr = new FileReader(" /data/data/com.example.steven.note/files/ " + fileName); int ch = 0; while ((ch = fr .read()) != -1) {       content += String.valueOf((char) ch); } Write FileOutputStream outputStream = openFileOutput(filename, Context.MODE_PRIVATE); outputStream .write(string.getBytes()); outputStream .close();