拖放上传 - 有趣的行为

发布于 2024-12-10 02:48:02 字数 2504 浏览 0 评论 0原文

请原谅问题标题,想不出如何描述这一点。

基本上,使用一些不同的脚本/教程,我已经成功地获得了一个有效的拖放、文件上传功能(适用于 Firefox、Safari 和 Chrome 用户)。

它工作得很好,但我对文件名和文件大小的显示方式有一个非常简单但困难的问题。

我没有得到这种格式:

<tr>
<th>File Name</th><th>100kb</th>
</tr>
<tr>
<td>File 1</td><td>100kb</td>
</tr>
<tr>
<td>File 2</td><td>145kb</td>
</tr>

我得到的是这样的:

<tr>
<th>File Name</th><th>100kb</th>
</tr>
<tr>
<td>&nbsp;</td><td>&nbsp;</td> ///// Annoying blank table row!!!
</tr>
<tr>
<td>File 1</td><td>100kb</td>
</tr>
<tr>
<td>File 2</td><td>145kb</td>
</tr>

我认为这发生在我的 Javascript 中的某个地方,尽管我以前从未使用过 所以也可能是这样。我确实阅读了有关 的文档,默认情况下,它不应该影响我的表格布局。

这是我的代码:

<table width="100%" id="uploadInfoTbl">
    <tr>
      <th>File</th>
      <th>Size</th>
    </tr>
  <tbody id="uploadQueue">
    <tr class="uploadTemplate">
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>

<script>
createFileQueue : function(files)
{
  $('tbody#uploadQueue').find('tr').not('.uploadTemplate').remove();

  $(files).each(
    function(key, file) {
      var tr = $('tr.uploadTemplate').clone();

      tr.removeClass('uploadTemplate');
      tr.attr('id', 'file-' + key);

      if (file.type.match(new RegExp('/image.*/')) && FileReader) {
        // Displaying a thumbnail doesn't seem to work in Safari
        var img = document.createElement('img');
        img.file = value;
        img.classList.add('thumbnail');

        // tr.find('tr').eq(0).html(img);

        var reader = new FileReader();

        reader.onload = (function(img) {
          return function(e) {
            img.src = e.target.result;
          };
        })(img);

        reader.readAsDataURL(value);
      }

      tr.find('td').eq(0).html(file.name);
      tr.find('td').eq(1).html(dnd.getFileSize(file.size));

      $('tbody#uploadQueue').append(tr);
    }
  );
},
</script>

有人能看出是什么导致了这个额外的 出现吗?我的脚本的其余部分我理解并且可以修复,但我真的还没有得到这个新的 HTML5 文件 API 以及如何打印它的结果。

另外,有人知道如何重置文件名和文件大小。我问是因为每次上传后,我都会将更多文件拖到我的放置区,并且会显示以前选择的文件以及新文件,并不断积累,直到我刷新浏览器?

预先非常感谢

Excuse the question title, couldn't think how to describe this..

Basically, using a few different scripts/tutorials, I have managed to get a working drag and drop, file upload feature together (for Firefox, Safari and Chrome users).

It works really well but I have a really simple but hard problem with the way the file name and file size are displayed.

Instead of getting this format:

<tr>
<th>File Name</th><th>100kb</th>
</tr>
<tr>
<td>File 1</td><td>100kb</td>
</tr>
<tr>
<td>File 2</td><td>145kb</td>
</tr>

I am getting this instead:

<tr>
<th>File Name</th><th>100kb</th>
</tr>
<tr>
<td> </td><td> </td> ///// Annoying blank table row!!!
</tr>
<tr>
<td>File 1</td><td>100kb</td>
</tr>
<tr>
<td>File 2</td><td>145kb</td>
</tr>

This is happening somewhere in my Javascript I think, although I have never used <TBODY> before so could be this also. I did read docs regarding <TBODY> and by default, it should NOT affect my table layout.

This is my code:

<table width="100%" id="uploadInfoTbl">
    <tr>
      <th>File</th>
      <th>Size</th>
    </tr>
  <tbody id="uploadQueue">
    <tr class="uploadTemplate">
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>

<script>
createFileQueue : function(files)
{
  $('tbody#uploadQueue').find('tr').not('.uploadTemplate').remove();

  $(files).each(
    function(key, file) {
      var tr = $('tr.uploadTemplate').clone();

      tr.removeClass('uploadTemplate');
      tr.attr('id', 'file-' + key);

      if (file.type.match(new RegExp('/image.*/')) && FileReader) {
        // Displaying a thumbnail doesn't seem to work in Safari
        var img = document.createElement('img');
        img.file = value;
        img.classList.add('thumbnail');

        // tr.find('tr').eq(0).html(img);

        var reader = new FileReader();

        reader.onload = (function(img) {
          return function(e) {
            img.src = e.target.result;
          };
        })(img);

        reader.readAsDataURL(value);
      }

      tr.find('td').eq(0).html(file.name);
      tr.find('td').eq(1).html(dnd.getFileSize(file.size));

      $('tbody#uploadQueue').append(tr);
    }
  );
},
</script>

Can anybody see what is causing this extra <TR> to appear? The rest of my script I understand and can fix but I really don't get this new HTML5 File API just yet and how to print the results from it.

Also, does anybody know how to reset the filenames and file sizes. I ask because after each upload, I drag more files to my drop-zone and the previous selected files are displayed, as well as the new ones, and keeps building up until I refresh the browser?

Many thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香橙ぽ 2024-12-17 02:48:02

正如@Gerben 所说,“烦人的空白表格行!!!”看起来与您从未在脚本中删除的 tr.uploadTemplate 非常相似。尝试更改您的模板

<tr class="uploadTemplate">
  <td>TEMPLATE</td>
  <td>TEMPLATE</td>
</tr>

以确认这一点。

如果您实际上在某处删除了模板行,并且空白行确实是由您的 JS 添加的,那么我首先在​​文件枚举器中添加 console.log() 调用(function(key, file))看看是否会再叫一次加时赛。如果是,则问题出在您发布的代码之外 - 您必须检查 files 值的来源。

As @Gerben said, the "Annoying blank table row!!!" looks very similar to the tr.uploadTemplate you never remove in your script. Try changing your template to

<tr class="uploadTemplate">
  <td>TEMPLATE</td>
  <td>TEMPLATE</td>
</tr>

in order to confirm that.

If you actually remove the template row somewhere and the blank row is indeed added by your JS, I'd start by adding console.log() calls in your file enumerator (function(key, file)) to see if it's called one extra time. If it is, the problem is outside the code you posted -- you have to check where files value comes from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文