SORU
27 AĞUSTOS 2010, Cuma


Nasıl bir yerel disk aç Javascript ile dosya?

İle dosyayı açmaya çalıştım

window.open("file:///D:/Hello.txt");

Tarayıcı yerel bir dosya bu şekilde, muhtemelen güvenlik nedeniyle açılış izin vermiyor. İstemci tarafında dosya veri kullanmak istiyorum. Nasıl Javascript yerel dosya okuyabilir miyim?

CEVAP
10 EKİM 2014, Cuma


İşte size bir örnek FileReader kullanarak:

function readSingleFile(e) {
  var file = e.target.files[0];
  if (!file) {
    return;
  }
  var reader = new FileReader();
  reader.onload = function(e) {
    var contents = e.target.result;
    displayContents(contents);
  };
  reader.readAsText(file);
}

function displayContents(contents) {
  var element = document.getElementById('file-content');
  element.innerHTML = contents;
}

document.getElementById('file-input')
  .addEventListener('change', readSingleFile, false);
<input type="file" id="file-input" />
<h3>Contents of the file:</h3>
<pre id="file-content"></pre>


Gözlük

http://dev.w3.org/2006/webapi/FileAPI/

Tarayıcı uyumluluk

  • YANİ 10
  • Firefox 3.6
  • Chrome 13
  • Safari 6.1

http://caniuse.com/#feat=fileapi

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Flohoo

    Flohoo

    12 EYLÜL 2009
  • RobertDuskin

    RobertDuskin

    12 HAZİRAN 2008
  • Vsauce

    Vsauce

    30 Temmuz 2007