处理Flask文件上传/下载中的编码 [英] Dealing with encoding in Flask file uploads/downloads

查看:191
本文介绍了处理Flask文件上传/下载中的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个react客户端,它将用户输入作为文件接收并将其发送到我的远程Flask服务器进行存储。我以Werkzeug FileStorage对象的形式发送文件,在远程服务器中,我用file.save(path)存储它。在react客户端我试图建立一种从服务器下载文件的方法,但是我遇到了问题。目前我的程序正在下载.txt文件。我可以通过获取javascript请求来执行此操作:

I have a react client that takes in user input as a file and sends it to my remote Flask server for storage. I send the file in the form of a Werkzeug FileStorage object and in the remote server I store it with file.save(path). In the react client I'm trying to build a way to download the file from the server, however I'm running into problems. Currently my program is working for downloading .txt files. I'm able to do this though a fetch javascript request:

 fetch(FETCH_URL, {
  method: 'POST',
  body: data,
  headers: {
    'Content-Type': 'application/json'
  }
}).then((response) => {
    var a = response.body.getReader();
    a.read().then(({ done, value }) => {
        saveAsFile(new TextDecoder("utf-8").decode(value), 'filename.txt');
      }
    );
});


function saveAsFile(text, filename) {
  const type = 'application/text'; // modify or get it from response
  const blob = new Blob([text], {type});
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = filename;
  a.click(); 
}

感谢我在这篇文章中提供的一些帮助:从flask远程服务器下载反应客户端文件

Thanks to some help I got in this post: Download file in react client from flask remote server

我知道这段代码专门用于基于传入Blob的类型的.txt文件,但前端不是真正的问题。

I know this code is specifically made to work only with .txt files based on the type being passed in to Blob, but the front end is not the real problem.

真正的问题是在我的远程烧瓶服务器中,以下代码是烧瓶服务器中调用的内容:

The real problem is in my remote flask server, the following code is what is called in the flask server:

 with open(filename, 'r') as f:
     contents = f.read()
     return contents

我尝试返回文件本身,但服务器出错:

I tried returning the file itself but the server gives an error:

"ValueError: I/O operation on closed file." 

所以我决定返回文件内容,如上所示。

So I decided to return the contents of the file as shown above.

当我尝试获取文件例如download.jpeg时出现问题。读取该文件会出现以下错误:

The problem arises when I try to get a file for example "download.jpeg". Reading the file gives the following error:

"UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte" 

根据我的理解,Flask只使用'utf-8'并且我假设这意味着服务器中的文件采用'utf-8'编码。

From what I understand Flask works exclusively with 'utf-8' and I assume this means the file in the server is on 'utf-8' encoded.

是否有人对解决方案或解决方法有任何建议或指导可能是一种在我将其保存在服务器上时更改文件编码的方法或其他可以帮助我的方法我正在尝试做什么?

Does anyone have a suggestion or guidance on a solution or a workaround maybe a way to change the files encoding when I save it on the server or something else that could help me with what I'm trying to do?

推荐答案

获取响应 blob()将响应直接转换为blob,因此您不必读取流,您不必查找它的内容类型或任何内容。试试下面的解决方案。

Fetch's Response has blob() to convert the response directly to blob, so you don't have to read the stream, you don't have to find out it's content type or anything. Just try the below solution.

fetch(FETCH_URL, {
  method: 'POST',
  body: data,
  headers: {
    'Content-Type': 'application/json'
  }
}).then((response) => {
      response.blob().then((blob) => {
         saveBlob(blob, 'filename');
      });
});


function saveBlob(blob, filename) {
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = filename;
  a.click(); 
}

这篇关于处理Flask文件上传/下载中的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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