소스 검색

getObjectURL for file demo

Michelle Bu 12 년 전
부모
커밋
5c7c3a0101
1개의 변경된 파일8개의 추가작업 그리고 2개의 파일을 삭제
  1. 8 2
      examples/chat.html

+ 8 - 2
examples/chat.html

@@ -55,8 +55,14 @@ function connect(c) {
         });
   } else if (c.label === 'file') {
     c.on('data', function(data) {
-      $('#' + c.peer).find('.messages').append('<div><span class="file">' +
-          c.peer + ' has sent you a file.</span></div>');
+      // If we're getting a file, create a URL for it.
+      if (data.constructor === ArrayBuffer) {
+        var dataView = new Uint8Array(data);
+        var dataBlob = new Blob([dataView]);
+        var url = window.URL.createObjectURL(dataBlob);
+        $('#' + c.peer).find('.messages').append('<div><span class="file">' +
+            c.peer + ' has sent you a <a target="_blank" href="' + url + '">file</a>.</span></div>');
+      }
     });
   }
 }