如何通过put请求从上传文件中获取文件名

2024-04-19 05:06:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用flaskrestplus开发API。其中一个端点处理mp3wav格式的音频文件上传。根据PUT request to upload a file not working in Flask,由{}上载的文件位于request.data或{}。我就是这么做的:

@ns.route('/upload')
class AudioUpload(Resource):
    def put(self):
        now = datetime.now()
        filename = now.strftime("%Y%m%d_%H%M%S") + ".mp3"
        cwd = os.getcwd()
        filepath = os.path.join(cwd, filename)
        with open(filepath, 'wb') as f:
            f.write(request.stream.read())
        return filepath

我将文件另存为mp3。但是,有时文件以wav的形式出现。有没有一种方法可以像postrequest一样从put请求获取原始文件名:

^{pr2}$

Tags: 文件apiputosrequestplusfilename端点