6257rv7 发表于 2024-6-29 16:30:19

HTML5 进阶系列:文件上传下载


    <h2 style="color: black; text-align: left; margin-bottom: 10px;">前言</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">HTML5 中<span style="color: black;">供给</span>的文件API在前端中有着丰富的应用,上传、下载、读取内容等在<span style="color: black;">平常</span>的交互中很<span style="color: black;">平常</span>。<span style="color: black;">况且</span>在各个浏览器的兼容<span style="color: black;">亦</span>比较好,<span style="color: black;">包含</span>移动端,除了 IE 只支持 IE10 以上的版本。想要更好地<span style="color: black;">把握</span>好操作文件的功能,先要熟悉<span style="color: black;">每一个</span>API。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">FileList 对象和 file 对象</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">HTML 中的 input 标签有个 multiple 属性,<span style="color: black;">准许</span>用户<span style="color: black;">选取</span>多个文件,FileList对象则<span style="color: black;">便是</span><span style="color: black;">暗示</span>用户<span style="color: black;">选取</span>的文件列表。这个列表中的每一个文件,<span style="color: black;">便是</span>一个 file 对象。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">file 对象的属性:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">name : 文件名,不<span style="color: black;">包括</span>路径。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">type : 文件类型。<span style="color: black;">照片</span>类型的文件都会以 image/ 开头,<span style="color: black;">能够</span>由此来限制只<span style="color: black;">准许</span>上传<span style="color: black;">照片</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">size : 文件<span style="color: black;">体积</span>。<span style="color: black;">能够</span><span style="color: black;">按照</span>文件<span style="color: black;">体积</span>来进行其他操作。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">lastModified : 文件最后修改的时间。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">input</span> <span style="color: black;">type</span><span style="color: black;">=</span><span style="color: black;">"file"</span> <span style="color: black;">id</span><span style="color: black;">=</span><span style="color: black;">"files"</span> <span style="color: black;">multiple</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;</span><span style="color: black;">script</span><span style="color: black;">&gt;</span>
      <span style="color: black;">var</span> <span style="color: black;">elem</span> <span style="color: black;">=</span> <span style="color: black;">document</span><span style="color: black;">.</span><span style="color: black;">getElementById</span><span style="color: black;">(</span><span style="color: black;">files</span><span style="color: black;">);</span>
      <span style="color: black;">elem</span><span style="color: black;">.</span><span style="color: black;">onchange</span> <span style="color: black;">=</span> <span style="color: black;">function</span> <span style="color: black;">(</span><span style="color: black;">event</span><span style="color: black;">)</span> <span style="color: black;">{</span>
      <span style="color: black;">var</span> <span style="color: black;">files</span> <span style="color: black;">=</span> <span style="color: black;">event</span><span style="color: black;">.</span><span style="color: black;">target</span><span style="color: black;">.</span><span style="color: black;">files</span><span style="color: black;">;</span>
      <span style="color: black;">for</span> <span style="color: black;">(</span><span style="color: black;">var</span> <span style="color: black;">i</span> <span style="color: black;">=</span> <span style="color: black;">0</span><span style="color: black;">;</span> <span style="color: black;">i</span> <span style="color: black;">&lt;</span> <span style="color: black;">files</span><span style="color: black;">.</span><span style="color: black;">length</span><span style="color: black;">;</span> <span style="color: black;">i</span><span style="color: black;">++</span><span style="color: black;">)</span> <span style="color: black;">{</span>
      <span style="color: black;">// 文件类型为 image 并且文件<span style="color: black;">体积</span><span style="color: black;">少于</span> 200kb
      </span> <span style="color: black;">if</span><span style="color: black;">(</span><span style="color: black;">files</span><span style="color: black;">[</span><span style="color: black;">i</span><span style="color: black;">].</span><span style="color: black;">type</span><span style="color: black;">.</span><span style="color: black;">indexOf</span><span style="color: black;">(</span><span style="color: black;">image/</span><span style="color: black;">)</span> <span style="color: black;">!==</span> <span style="color: black;">-</span><span style="color: black;">1</span> <span style="color: black;">&amp;&amp;</span> <span style="color: black;">files</span><span style="color: black;">[</span><span style="color: black;">i</span><span style="color: black;">].</span><span style="color: black;">size</span> <span style="color: black;">&lt;</span> <span style="color: black;">204800</span><span style="color: black;">){</span>
      <span style="color: black;">console</span><span style="color: black;">.</span><span style="color: black;">log</span><span style="color: black;">(</span><span style="color: black;">files</span><span style="color: black;">[</span><span style="color: black;">i</span><span style="color: black;">].</span><span style="color: black;">name</span><span style="color: black;">);</span>
      <span style="color: black;">}</span>
      <span style="color: black;">}</span>
      <span style="color: black;">}</span>
      <span style="color: black;">&lt;/</span><span style="color: black;">script</span><span style="color: black;">&gt;</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">input 中有个 accept 属性,<span style="color: black;">能够</span>用来规定能够<span style="color: black;">经过</span>文件上传进行提交的文件类型。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">accept="image/*" <span style="color: black;">能够</span>用来限制只<span style="color: black;">准许</span>上传图像格式。<span style="color: black;">然则</span>在 Webkit 浏览器下却<span style="color: black;">显现</span>了响应滞慢的问题,要等上好几秒才弹出文件<span style="color: black;">选取</span>框。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">处理</span><span style="color: black;">办法</span><span style="color: black;">便是</span>将 * 通配符改为指定的 MIME 类型。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">input</span> <span style="color: black;">type</span><span style="color: black;">=</span><span style="color: black;">"file"</span> <span style="color: black;">accept</span><span style="color: black;">=</span><span style="color: black;">"image/gif,image/jpeg,image/jpg,image/png"</span><span style="color: black;">&gt;</span>
    </div>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">Blob 对象</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Blob 对象相当于一个容器,<span style="color: black;">能够</span>用于存放二进制数据。它有两个属性,size 属性<span style="color: black;">暗示</span>字节长度,type 属性<span style="color: black;">暗示</span> MIME 类型。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">怎样</span>创建</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Blob 对象<span style="color: black;">能够</span><span style="color: black;">运用</span> Blob() 构造函数来创建。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">var</span> <span style="color: black;">blob</span> <span style="color: black;">=</span> <span style="color: black;">new</span> <span style="color: black;">Blob</span><span style="color: black;">([</span><span style="color: black;">hello</span><span style="color: black;">],</span> <span style="color: black;">{</span><span style="color: black;">type</span><span style="color: black;">:</span><span style="color: black;">"text/plain"</span><span style="color: black;">});</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Blob 构造函数中的<span style="color: black;">第1</span>个参数是一个数组,<span style="color: black;">能够</span>存放 ArrayBuffer对象、ArrayBufferView 对象、Blob对象和字符串。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Blob 对象<span style="color: black;">能够</span><span style="color: black;">经过</span> slice() <span style="color: black;">办法</span>来返回一个新的 Blob 对象。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">var</span> <span style="color: black;">newblob</span> <span style="color: black;">=</span> <span style="color: black;">blob</span><span style="color: black;">.</span><span style="color: black;">slice</span><span style="color: black;">(</span><span style="color: black;">0</span><span style="color: black;">,</span><span style="color: black;">5</span><span style="color: black;">,</span> <span style="color: black;">{</span><span style="color: black;">type</span><span style="color: black;">:</span><span style="color: black;">"text/plain"</span><span style="color: black;">});</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">slice() <span style="color: black;">办法</span><span style="color: black;">运用</span>三个参数,均为可选。<span style="color: black;">第1</span>个参数<span style="color: black;">表率</span>要从Blob对象中的二进制数据的<span style="color: black;">初始</span>位置<span style="color: black;">起始</span>复制,第二个参数<span style="color: black;">表率</span>复制的结束位置,第三个参数为 Blob 对象的 MIME 类型。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">canvas.toBlob() <span style="color: black;">亦</span><span style="color: black;">能够</span>创建 Blob 对象。toBlob() <span style="color: black;">运用</span>三个参数,<span style="color: black;">第1</span>个为回调函数,第二个为<span style="color: black;">照片</span>类型,默认为 image/png,第三个为<span style="color: black;">照片</span>质量,值在0到1之间。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">var</span> <span style="color: black;">canvas</span> <span style="color: black;">=</span> <span style="color: black;">document</span><span style="color: black;">.</span><span style="color: black;">getElementById</span><span style="color: black;">(</span><span style="color: black;">canvas</span><span style="color: black;">);</span>
      <span style="color: black;">canvas</span><span style="color: black;">.</span><span style="color: black;">toBlob</span><span style="color: black;">(</span><span style="color: black;">function</span><span style="color: black;">(</span><span style="color: black;">blob</span><span style="color: black;">){</span> <span style="color: black;">console</span><span style="color: black;">.</span><span style="color: black;">log</span><span style="color: black;">(</span><span style="color: black;">blob</span><span style="color: black;">);</span> <span style="color: black;">},</span> <span style="color: black;">"image/jpeg"</span><span style="color: black;">,</span> <span style="color: black;">0.5</span><span style="color: black;">);</span>
    </div>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">下载文件</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Blod 对象<span style="color: black;">能够</span><span style="color: black;">经过</span> window.URL 对象生成一个网络<span style="color: black;">位置</span>,结合 a 标签的 download 属性来实现下载文件功能。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如</span>把 canvas 下载为一个<span style="color: black;">照片</span>文件。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">var</span> <span style="color: black;">canvas</span> <span style="color: black;">=</span> <span style="color: black;">document</span><span style="color: black;">.</span><span style="color: black;">getElementById</span><span style="color: black;">(</span><span style="color: black;">canvas</span><span style="color: black;">);</span>
      <span style="color: black;">canvas</span><span style="color: black;">.</span><span style="color: black;">toBlob</span><span style="color: black;">(</span>
    </div>




4zhvml8 发表于 2024-9-28 07:58:41

请问、你好、求解、谁知道等。

1fy07h 发表于 2024-10-11 07:16:32

交流如星光璀璨,点亮思想夜空。

nqkk58 发表于 2024-10-23 14:22:03

同意、说得对、没错、我也是这么想的等。

7wu1wm0 发表于 2024-11-13 03:21:51

我深感你的理解与共鸣,愿对话长流。
页: [1]
查看完整版本: HTML5 进阶系列:文件上传下载