Javascript - 保存到磁盘的文件卡在Chrome内存中 [英] Javascript - File saved to disk is stuck in Chrome's memory

查看:146
本文介绍了Javascript - 保存到磁盘的文件卡在Chrome内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function saveFile(str,part){
var textFileAsBlob = new Blob [str],{type:text / plain});
var fileNameToSaveAs =解析音频 - 部分+部分;

var downloadLink = document.createElement(a);
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML =下载档案;

if(window.URL!= null)
{
// Chrome允许链接点击
//而不需要实际添加到DOM。
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
}
downloadLink.click();
}

它可以正常工作,除了Chrome的这个问题:blob的足迹或任何保存在Chrome主进程内存中的内容。当打开下载窗口时,整个blob(在我的情况下为250MB!)被复制到主进程的内存中。这有点不好,因为如果我保存多个文件,我最终会填充内存高达750MB,并在此时停止下载带有未找到错误的文件。图: http://i.stack.imgur.com/j5PUn.jpg



我在做一些愚蠢的错误还是Chrome的错?我可以清理Chrome的内存来摆脱这个问题吗?

/ questions / 29878506 /#comment47881112_29878506>我的评论似乎是您要找的答案,我已将其作为实际答案




您没有在点击后发布 blob网址,这意味着GC无法摆脱 blob p>

  //点击
后的URL.revokeObjectURL(downloadLink.href);


I have this code:

function saveFile(str, part) {
  var textFileAsBlob = new Blob([str], {type:"text/plain"});
  var fileNameToSaveAs = "Parsed audio - part "+part;

  var downloadLink = document.createElement("a");
  downloadLink.download = fileNameToSaveAs;
  downloadLink.innerHTML = "Download File";

  if (window.URL != null)
  {
      // Chrome allows the link to be clicked
      // without actually adding it to the DOM.
      downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
  }
  downloadLink.click(); 
}

It works okay, except this one problem with Chrome: The blob's "footprint" or whatever is kept in Chrome's main process' memory. When download window is opened, the whole blob (250MB in my case!) is copied into main process' memory. That's kinda bad since if I save multiple files, I eventually fill memory up to 750MB, and at that point chrome stops downloading files with "Not found" error. Pic: http://i.stack.imgur.com/j5PUn.jpg

Am I doing some stupid mistake or is this Chrome's fault? Can I clean Chrome's memory to get rid of this problem?

解决方案

As my comment seemed to be the answer you were looking for, I've put it as an actual answer


You're not releasing the blob URL after the click, which means the GC can't get rid of the blob

// after the click
URL.revokeObjectURL(downloadLink.href);

这篇关于Javascript - 保存到磁盘的文件卡在Chrome内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆