te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How do I save a file on my desktop using ReactJS? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I save a file on my desktop using ReactJS? - Stack Overflow

programmeradmin1浏览0评论

Right now I am creating a voice search feature. So the plan is I will record the audio using ReactJS, convert the Blob file into a .wav file. Then once the file is created I will save it locally on my desktop. Once the file is on the desktop, I will use the OS module in python to access the file on my desktop to interpret the audio file into text.

So everything is essentially done, the only problem that I am having is I can't figure out how to use ReactJS to save the file on my desktop.

Can someone please help me with this?

Right now I am creating a voice search feature. So the plan is I will record the audio using ReactJS, convert the Blob file into a .wav file. Then once the file is created I will save it locally on my desktop. Once the file is on the desktop, I will use the OS module in python to access the file on my desktop to interpret the audio file into text.

So everything is essentially done, the only problem that I am having is I can't figure out how to use ReactJS to save the file on my desktop.

Can someone please help me with this?

Share Improve this question asked Feb 6, 2021 at 15:08 Tim ScottTim Scott 771 gold badge2 silver badges9 bronze badges 5
  • 2 stackoverflow./questions/5192917/… second answer down <a download="name_of_downloaded_file" href="path/to/the/download/file"> Clicking on this link will force download the file</a> – gugateider Commented Feb 6, 2021 at 15:14
  • Did you see that: medium./javascript-in-plain-english/… ? – yovchokalev Commented Feb 6, 2021 at 15:25
  • Saving a file from the browser to anywhere but the downloads folder is a huge security hole, and it's not allowed. I don't think you'll be able to save to the Desktop. Here's a technique that will save to Downloads. – terrymorse Commented Feb 6, 2021 at 15:39
  • @terrymorse eventually I will use POST to put it on a server but for now I just want it locally. – Tim Scott Commented Feb 6, 2021 at 15:52
  • Why not just set up a localhost server? You can save any file to disk with a server, and you'll have written some server code for future use. – terrymorse Commented Feb 6, 2021 at 18:23
Add a ment  | 

3 Answers 3

Reset to default 6

Refer the more detail in web.dev and about MDN blob

Following code will work. (Try loading in chrome dev console or similar)

const saveFile = async (blob) => {
  const a = document.createElement('a');
  a.download = 'my-file.txt';
  a.href = URL.createObjectURL(blob);
  a.addEventListener('click', (e) => {
    setTimeout(() => URL.revokeObjectURL(a.href), 30 * 1000);
  });
  a.click();
};



const obj = {hello: 'world'};
const blob = new Blob([JSON.stringify(obj, null, 2)], {type : 'application/json'});

saveFile(blob);

// With react

<div onClick={() => saveFile(blob)}> save file </div>

You can use this https://www.npmjs./package/file-saver then save the file like this: saveAs(blob, 'filename').

once the file is created I will save it locally on my desktop

It is not possible to save a file from the browser to the Desktop, as this would be a huge security vulnerability.

However, you can save a file into the Downloads folder, if the user authorizes it.

This example saves a "Hello World!" text file to the Downloads folder.

Other data types can be saved, using the "data:" URL.

// save a text file named 'hello-world.txt' to Downloads folder

document.querySelector('button').onclick = function(evt) {
  const data = `data:,Hello%2C%20World!`;
  const filename = 'hello-world.txt';
  const aTag = document.createElement('a');

  aTag.href = data;
  aTag.download = filename;
  aTag.click();
};
<button>Save Hello World!</button>

发布评论

评论列表(0)

  1. 暂无评论