如何从Flask在React Native中下载文件

huangapple go评论67阅读模式
英文:

how to download file from flask in react native

问题

我正在制作一个小型应用程序,用于从YouTube下载视频。我在前端使用React Native,后端使用Python Flask。
以下是在服务器上下载视频并将其作为附件返回给客户端的代码:

from flask import Flask, send_file
import vid_request

app = Flask(__name)

@app.route("/<url>")
def download(url):
    path = vid_request.download(url, "/tempDownloads")
    return send_file(path, as_attachment=True)

if __name__ == "__main__":
    app.run(host="0.0.0.0")

// vid_request 文件

import pytube

def download(url, path):
    url = "https://www.youtube.com/watch?v=" + url
    youtubeObject = pytube.YouTube(url).streams.filter(only_audio=True).first()
    out_file = youtubeObject.download(output_path=path)

    return out_file

当我在浏览器中访问URL并输入YouTube链接时,这段代码可以正常工作。但当我在React Native中使用以下代码时:

await fetch(`http://192.168.0.12:5000/${input}`);

它不起作用并显示错误。是否有更好的方法在服务器端发送文件,或者从客户端端下载文件的更好方法?

英文:

I am making a small app that downloads videos from youtube. I am using react native for the frontend, and python flask for the backend.
Here is the code on the server that downloads the video to the server and returns it to the client as an attachment

from flask import Flask, send_file
import vid_request

app = Flask(__name__)

@app.route(&quot;/&lt;url&gt;&quot;)
def download(url):
    path = vid_request.download(url, r&quot;\tempDownloads&quot;)
    return send_file(path, as_attachment=True)

if __name__ == &quot;__main__&quot;:
    app.run(host=&quot;0.0.0.0&quot;)

// vid_request file

import pytube

def download(url, path):
    url = &quot;https://www.youtube.com/watch?v=&quot;+url
    youtubeObject = pytube.YouTube(url).streams.filter(only_audio=True).first()
    out_file = youtubeObject.download(output_path=path)

    return out_file

This works when I go to the url and enter the youtube link, but when I do it from react native using this

await fetch(`http://192.168.0.12:5000/${input}`);

It doesn't work and gives an error, is there a better way for sending the file on the server side, or better way of downloading from the client side?

答案1

得分: 1

  1. npx expo install expo-file-system
  2. 如果是原生 React Native,还需要按照以下步骤进行设置:https://github.com/expo/expo/tree/sdk-47/packages/expo-file-system
  3. 修改以下部分:
import { downloadAsync, documentDirectory } from 'expo-file-system';
import { Button, View } from 'react-native';

export default function App() {
  const download = async () => {
    console.log("downloading");
    const { uri: localUri } = await downloadAsync('http://192.168.0.12:5000/video.mp4', documentDirectory + 'video.mp4');
    console.log("download complete. File " + localUri);
  }
  return (
    <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
      <Button title="download" onPress={download}></Button>
    </View>
  );
}

完整文档

英文:
  1. npx expo install expo-file-system
  2. If its bare React Native, you need to additionally follow this steps:
    https://github.com/expo/expo/tree/sdk-47/packages/expo-file-system
  3. Amend the following
import { downloadAsync, documentDirectory} from &#39;expo-file-system&#39;;
import { Button, View } from &#39;react-native&#39;;

export default function App() {
  const download = async () =&gt; {
    console.log(&quot;downloading&quot;);
    const { uri: localUri } = await downloadAsync(&#39;http://192.168.0.12:5000/video.mp4&#39;, documentDirectory + &#39;video.mp4&#39;);
    console.log(&quot;download complete. File &quot;+localUri);
  }
  return (
    &lt;View style={{flex:1, alignItems: &quot;center&quot;, justifyContent: &quot;center&quot;}}&gt;
      &lt;Button title=&quot;download&quot; onPress={download}&gt;&lt;/Button&gt;
    &lt;/View&gt;
  );
}

Full Documentation

huangapple
  • 本文由 发表于 2023年2月14日 06:10:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定