Flask-获取上传文件的名称减去文件扩展名

时间:2018-11-01 09:22:39

标签: python python-3.x flask werkzeug

我有一个表单,可以在多个字段中上传多个文件

例如: 我有一个名为PR1,另一个Pr2和PR3的字段, 在每个此字段中,我都可以上传(或不上传)多个文件,上传方面效果很好:

#include<iostream>
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
using namespace std;
int main(){

    int sockid=socket(AF_INET,SOCK_STREAM,0);
    if(sockid<0){
        cout<<"failed socket";
    }
    struct sockaddr_in server, client;
    int cz=sizeof(client);
    server.sin_family=AF_INET;
    server.sin_port=htons(9999);
    server.sin_addr.s_addr=htonl(INADDR_ANY);

    if(bind(sockid,(struct sockaddr*)&server, sizeof(server))<0){
        cout<<"Failed binding";
        return 0;
    }
    cout<<"binded\n";

    if(listen(sockid,3)<0){//
        cout<<"\nFailed Listening";
        return 0;
    }

    int client_socket=accept(sockid,(struct sockaddr*)&client, (socklen_t*)&cz);

    if(client_socket<0){
        cout<<"Failed connecting";
        return 0;
    }
    cout<<"Connected....\n";

    char buff[1024]={0};

    cout<<"enter message: ";
    cin>>buff;
    if(send(client_socket,buff,strlen(buff),0)<0){
        cout<<"\nFailed sending\n";
        return 0;
    }
    cout<<"Message sent";




    return 0;
}

因此使用这种方法的结果例如是:

#include<arpa/inet.h>
#include<netdb.h>
#include<iostream>
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<netinet/in.h>
using namespace std;
int main(){

    int sockid=socket(AF_INET,SOCK_STREAM,0);
    if(sockid<0){
        cout<<"failed socket";
    }
    struct sockaddr_in  client;

    int cz=sizeof(client);
    client.sin_family=AF_INET;
    client.sin_port=htons(9999);
    client.sin_addr.s_addr=INADDR_ANY;




    int server_socket=connect(sockid,(struct sockaddr*)&client, sizeof(client));

    if(server_socket<0){
        cout<<"Failed connecting";
        return 0;
    }
    cout<<"Connected\n";

    char buff[250];//

    recv(sockid,buff,1024,0);
    cout<<"Received msg: "<<buff;

    return 0;
}

这时我想只用files = request.files for prodotti in files: print(prodotti) for f in request.files.getlist(prodotti): if prodotti == 'file_ordine': os.makedirs(os.path.join(app.instance_path, 'file_ordini'), exist_ok=True) f.save(os.path.join(app.instance_path, 'file_ordini', secure_filename(f.filename))) print(f) 的名称和文件扩展名来更新数据库Pr1 <FileStorage: 'FAIL #2.mp3' ('audio/mp3')> 行中的字段file,如何获取文件名?

1 个答案:

答案 0 :(得分:1)

它返回一个FileStorage对象,f是一个FileStorage对象,您可以从其中访问文件名,名称为FileStorage.filename

>>> from werkzeug.datastructures import FileStorage
>>> f = FileStorage(filename='Untitled.png')
>>> type(f)
<class 'werkzeug.datastructures.FileStorage'>
>>> f.filename
'Untitled.png'
>>> f.filename.split('.')
['Untitled', 'png']
>>> f.filename.split('.')[0]
'Untitled'
>>> 

app.py

import os
from flask import Flask, render_template, request
from werkzeug.utils import secure_filename

app = Flask(__name__)

app.config['SECRET_KEY'] = '^%huYtFd90;90jjj'
app.config['UPLOADED_PHOTOS'] = 'static'


@app.route('/upload', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST' and 'photo' in request.files:
        file = request.files['photo']
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOADED_PHOTOS'], filename))
        print(file.filename, type(file), file.filename.split('.')[0])
    return render_template('page.html')


if __name__ == "__main__":
    app.run(debug=True)

它打印出来:

untitled.png <class 'werkzeug.datastructures.FileStorage'> untitled
127.0.0.1 - - [01/Nov/2018 18:20:34] "POST /upload HTTP/1.1" 200 -