第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

上傳前顯示圖像預(yù)覽

上傳前顯示圖像預(yù)覽

素胚勾勒不出你 2019-07-16 18:19:43
上傳前顯示圖像預(yù)覽在我的HTML表單中,我使用類型文件進(jìn)行輸入,例如: <input type="file" multiple>然后單擊輸入按鈕選擇多個(gè)文件?,F(xiàn)在我想在提交表單之前顯示選定圖像的預(yù)覽。如何在HTML 5中做到這一點(diǎn)?
查看完整描述

3 回答

?
慕容3067478

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊

HTML 5附帶文件API規(guī)范,這允許您創(chuàng)建允許用戶在本地與文件交互的應(yīng)用程序;這意味著您可以加載文件并在瀏覽器中呈現(xiàn)它們,而無需實(shí)際上傳文件。文件API的一部分是文件閱讀器接口,它允許web應(yīng)用程序異步讀取文件的內(nèi)容。

下面是一個(gè)使用FileReader類將圖像讀取為DataURL,并通過設(shè)置src圖像標(biāo)記到數(shù)據(jù)URL的屬性:

html代碼:

<input type="file" id="files" /><img id="image" />

JavaScript代碼:

document.getElementById("files").onchange = function () {
    var reader = new FileReader();

    reader.onload = function (e) {
        // get loaded data and render thumbnail.
        document.getElementById("image").src = e.target.result;
    };

    // read the image file as a data URL.
    reader.readAsDataURL(this.files[0]);};

這是一篇關(guān)于在JavaScript中使用文件API.

下面的HTML示例中的代碼片段過濾掉用戶選擇的圖像,并將選定的文件呈現(xiàn)為多個(gè)縮略圖預(yù)覽:

function handleFileSelect(evt) {
    var files = evt.target.files;

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = 
          [
            '<img style="height: 75px; border: 1px solid #000; margin: 5px" src="', 
            e.target.result,
            '" title="', escape(theFile.name), 
            '"/>'
          ].join('');
          
          document.getElementById('list').insertBefore(span, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
<input type="file" id="files" multiple /><output id="list"></output>


查看完整回答
反對(duì) 回復(fù) 2019-07-16
  • 3 回答
  • 0 關(guān)注
  • 432 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)