简体   繁体   English

如何使用Flask提供文件,然后使用curl下载

[英]How to serve file with flask and then download it with curl

So I am trying to make an API with flask and then use it with javascript to display some images on the browser but I am running into problems with unit-testing whether the image sending (through Flask) works properly. 因此,我试图用flask创建一个API,然后将其与javascript一起使用,以在浏览器上显示一些图像,但是我遇到了单元测试(通过Flask)发送图像是否正常工作的问题。 This is the flask code I have right now 这是我现在拥有的烧瓶代码

@api.route("/pictures/<picture_name>")
def send_picture(picture_name):
    """Used to send the requested picture from the pictures folder."""
    return api.send_static_file(picture_name)

And then I am trying to download say for example an image puppy.png with the following command 然后我尝试使用以下命令下载例如图像puppy.png的图片

curl -O --request GET "http://localhost:8000/pictures/tank.png"

I can open the same link on my browser and see the file perfectly so I know the send_static_file function is working properly. 我可以在浏览器中打开相同的链接,并完美地查看文件,因此我知道send_static_file函数可以正常工作。 But the curl command above downloads the file in such a way that it can't be opened. 但是上面的curl命令以无法打开的方式下载文件。 Is there something else I am doing wrong here? 我在这里还做错什么吗?

I am not that familiar with Python and Flask yet... 我还不太熟悉Python和Flask ...

EDIT : Somehow the same thing works perfectly fine with wget so I am wondering if this is a problem with the way I am making the curl call. 编辑:以某种方式,同一件事可以与wget完美配合,所以我想知道这是否与我进行curl调用的方式有关。 Is there something I am doing wrong? 我做错什么了吗? Could someone explain how exactly something is downloaded via HTTP? 有人可以解释一下如何通过HTTP下载某些内容吗? As in are there any special headers to the get request? 如在其中,get请求有任何特殊的标头吗? If not then what is the response formatted like and how is the file stored? 如果不是,那么响应的格式是什么样的以及如何存储文件?

curl -X POST -F **file=@/home/developer/Documents/cloudish_csv/cloudish1.csv** http://127.0.0.1:5000/cloudish/daily_update?api_key=hqjjnynxtgqvkqzowdxpefdv

place your filepath in file field in curl request and use stringio to download the csv calling another extension like above without any file field.Example code attached.... 将您的文件路径放在curl请求中的file字段中,并使用stringio下载csv,调用上面提到的另一个扩展名,而没有任何file字段。附加示例代码。

class Clouddish_forecast(Resource):
   def get(self):
       output = StringIO()
       out = pd.read_csv("/home/developer/Downloads/vig.csv",sep=",")
       out.to_csv(output, index=False)
       return Response(output.getvalue(), mimetype="text/csv")

vig.csv will be downloaded by this return statement vig.csv将通过此return语句下载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM