【问题标题】:jQuery get fakepath for file input with "multiple" attribute to preview imagejQuery获取具有“多个”属性的文件输入的假路径以预览图像
【发布时间】:2018-09-24 07:56:24
【问题描述】:

我有输入文件(多个)用于上传图像。上传图像后,它将显示为缩略图和完整视图。我已经有URL.createObjectURL() 将图像渲染为blob,但是当图像数量更多时,它会稍微影响页面,因为每个图像都有 2 个用于缩略图和完整视图的 blob 数据。

对于单个文件上传,很容易让URL.createObjectURL() 用于缩略图和$(this).val() 为完整视图生成假路径。但我不知道如何通过多文件上传来做到这一点。

示例代码:

$('#imageBtn').on('change', function(){
    var inputFiles = this.files;
    $(inputFiles).each(function(index) {
       var reader = new FileReader();
       reader.readAsDataURL(inputFile);
       reader.onloadend = function(){
           RenderedPath = URL.createObjectURL(inputFile);
           FakePath = inputFile.val();
           // Some codes to populate the thumbnail and fullview
       }
    });
});

那么,如何获取每张上传图片的虚假路径?

【问题讨论】:

  • 你想用这个 fakePath 做什么?作为一个“假”路径,它通常是没有用的,而且自从 File API 出现以来,每个 File 对象都有自己的 name 属性。
  • @Kaiido fake path 足以显示可以减少负载的图像。
  • @sam 不,fakepath 完全没有用。我会尝试发布一个全面的答案。
  • @Kaiido 谢谢,抱歉我跛脚。我是脚本方面的新手。

标签: javascript jquery forms file-upload filereader


【解决方案1】:

我看不出你想用这个 fakePath 做什么。作为一个“fake”路径,它是没有用的。

从历史上看,(在 File API 之前),它是一种检索选定文件名称的方法,但现在我们有了 File API,此信息直接在 File 对象中提供。

所以如果你真的想自己构建它,比如input[type=file][multiple],那么你可以简单地将"C:\fakePath\"添加到文件的name

但再一次,您将无法通过此字符串执行任何操作。

还请注意,在您的代码中,您不会对 FileReader 的结果做任何事情,而且无论如何,此时您不需要它,因此请在此处删除它,因为它可能是您的内存问题的原因之一.
实际上,对于用户提供的文件,BlobURI 不会使用任何内存,它是指向存储在用户磁盘上的文件的简单指针,而将其作为 dataURI 读取实际上会将整个数据加载到内存中三倍。

所以对于你展示的部分,它可以简单地是

$('input[type="file"]').on('change', function() {
  var files = this.files;
  $(files).each(function(index, file) {
    // Still don't know why you want this...
    var fakepath = 'C:\\fakepath\\';
    $('body').append('<div>' +
      // build  a fake path string for each File
      '<p class="path">'+ fakepath + file.name + '</p>' +
      // all that is really needed to display the image
      '<img src="'+URL.createObjectURL(file)+'">' +
     '</div>');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" multiple>

现在,您在评论中声明您需要 FileReader 将其传递给 jsZip 库,如果您在谈论 this one,那么您必须知道 it accepts Blobs。所以你仍然不需要 FileReader。

var fileList = [];
$('input[type="file"]').on('change', function() {
  var files = this.files;
  $(files).each(function(index, file) {
    $('body').append($('<img>', {
      src: URL.createObjectURL(file),
      onload: function() { fileList.push(file); $('button').attr('disabled', false);},
      onerror: function() { $(this).remove(); }
    }));
  });
});

$('button').on('click', function() {
  var zip = new JSZip();
  fileList.forEach(function(file) {
    zip.file(file.name, file);
  });
  zip.generateAsync({
      type: "blob"
    })
    .then(function(blob) {
      saveAs(blob, "myfiles.zip");
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"></script>

<input type="file" multiple accept="image/*">
<button disabled>save as zip</button>

【讨论】:

  • 我尝试在文件名前添加“C:\fakePath\”,但图像未显示。你能提供文件API的链接吗?如果 jszip 接受 blob,那么你是对的,我不需要文件阅读器。感谢您提供信息。
  • @sam 我添加了一个生成 zip 文件的示例,以及 MDN 关于 File API 的文章的链接,这是一个非常庞大的 API。
猜你喜欢
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 2017-07-27
  • 2012-02-07
相关资源
最近更新 更多