File Access

As of version 2.0, FileAccess can break a file into chunks. Smaller chunks may speed uploads or help parallelize certain operations.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<html>
  <head><title>Files Chunks</title>
</head>

<body>
<p>
<div id="dropTitle">loading...</div>
<br>
<div id="lastDropSummary"></div>
<textarea rows="20" cols="80" id="lastDropContents"></textarea> 

<script src="http://bp.yahooapis.com/2.9.9/browserplus-min.js"></script>  
<script type="text/javascript">
function setDropAreaTitle(txt) {
  var div = document.getElementById("dropTitle");
  while (div.firstChild) {div.removeChild(div.firstChild);}
  div.appendChild(document.createTextNode(txt));
}

function hovering(hoverOn) {
  if (hoverOn) {setDropAreaTitle("drop it!");}
  else {setDropAreaTitle("drag a large file to the box below.");}
} 

function dropped(arg) {
  var textArea = document.getElementById("lastDropContents");

  var title = document.createElement("b");
  title.innerHTML = "Last drop Contents (" + arg[0].name + " - " + 
    arg[0].size + " bytes - " + arg[0].mimeType + ")<p>";
 
  // set summary text
  lds = document.getElementById("lastDropSummary");
  while (lds.firstChild) lds.removeChild(lds.firstChild);
  lds.appendChild(title);
  textArea.value = "reading...";

  BrowserPlus.FileAccess.chunk({ file: arg[0], chunkSize:65536 },
	function (rez) {
	  var contents = "", files, i, len, str;
	  if (rez.success) {
          files = rez.value;
          for (i = 0, len=files.length; i < len; i++) {
            contents += "File[" + i + "/" + len + "] = " + files[i].name + " (size=" + files[i].size + ")\n";
          }
	  } else {
		  contents = rez.error + ": " + rez.verboseError;
	  }      
	  textArea.value = contents;
	});
}

BrowserPlus.init(function(res) {
  if (res.success) {
   BrowserPlus.require({
      services: [
		{service: 'DragAndDrop', version: "1"},
		{service: 'FileAccess', version: "2", minversion:"2.0.1"}
	  ]},
      function(res) {
        if (res.success) {
          var dnd = BrowserPlus.DragAndDrop;
          dnd.AddDropTarget(
            {id: "lastDropContents"},
            function(res) {
              dnd.AttachCallbacks({
                id: "lastDropContents",
                hover: hovering,
                drop: dropped
              },
              function(){});  
            setDropAreaTitle("drag something to the box below.");
          });
        } else {
          alert("Error Loading DragAndDrop: " + res.error);
        }
      });
  } else {
    alert("Failed to initialize BrowserPlus: " + res.error);
  }
}); 
</script> 
</body>
</html>
Run Example

This example demonstrates how to break up a larger file into chunks. By default, 2MB chunks are created, but for the purpose of this demo, the chunk size is 64KB.