正在复制Flask应用程序文件夹中知道其web路径的文件



我已经使用生成了一个文件的web路径

URL.createObjectURL(event.target.files[0]);

后来,通过ajax调用,我将该链接传递给了Python(Flask(。现在,我很想知道如何使用该链接将文件复制到我正在开发的应用程序的文件夹中。使用Python。有人能帮我吗?

根据我的经验,我意识到最好通过特定的调用发送文件。就我个人而言,使用JavaScript我得到的文件如下:

const filePdfUploaded = document.querySelector('input[id="uploadFilePdf"]');
const fileTxtUploaded = document.querySelector('input[id="uploadFileTxt"]');
const filesUploaded = new FormData();
filesUploaded.append('pdf', filePdfUploaded.files[0]);
filesUploaded.append('txt', fileTxtUploaded.files[0]);
fetch('/documents', {
method: 'POST',
body: filesUploaded /* posting */
});

在服务器端,Flask提供如下功能:

@app.route('/documents', methods= ['POST'])
def addUploadedFiles():
if request.method == 'POST':
request.files['pdf'].save(os.path.join(app.instance_path[:-9],   #this
'static',                 #concerns
'data',                   #the destination
'documentazione Tot Pdf', #path
secure_filename(request.files['pdf'].filename)))
request.files['txt'].save(os.path.join(app.instance_path[:-9],   #this 
'static',                 #concerns
'data',                   #the destination
'documentazione txt',     #path 
secure_filename(request.files['txt'].filename)))
return ''

通过这种方式,可以接收文件并将其保存在当前项目的文件夹中

我希望我对你有所帮助。

最新更新