如何让 createObjectUrl() 在 react-router 中工作?

3

window.createObjectUrl() 会创建一个形如 blob:http://domain/generatedString 的URL,我想在 <iframe> 中使用此URI,但似乎不起作用,因为 react-router 没有匹配到任何规则时就会将我重定向到主页,因为它的开关如下:

<Switch>
  <Route exact path="/" component={Home} />
  <Route exact  path="/a" component={A} />
  <Route exact  path="/b" component={B} />
  <Route component={nomatch} />
</Switch>

我该如何为这些blob URI之一制定规则?

编辑: 好的,答案不是react-router,是其他东西搞乱了我的blob。不幸的是,我不记得是什么了。但是,现在你知道它不是react-router了 :+)

2个回答

6
当您在浏览器中打开链接时,请不要忘记在URL前面加上blob:。不要尝试:http://domain/generatedString,而是使用blob:http://domain/generatedString。这样您的React应用程序就不会被调用,也不会调用react router

0

对我来说,在JSX中它是这样工作的:

const url = window.URL.createObjectURL(someData)
const downloadLink = React.createElement('a', { download: 'filename.csv', href: url}, 'download')

return (downloadLink)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接