Home > other >  Upload Files and a String in same Flask Request
Upload Files and a String in same Flask Request

Time:01-21

I would like to upload files and a separate string containing an email address using flask.

I have it working to upload the files and have the email address stored on the server but would like to upload any email address from the client

test_files = {('dicom', open("Pre_1", "rb")),
              ('dicom', open("Pre_2", "rb")),
              ('dicom', open("Post_1", "rb")),
              ('dicom', open("Post_2", "rb"))}
response = requests.post("MyWebsite.com", files = test_files)

Works for me. But I am not sure how to add email = "[email protected]" to the upload request?

This is what I have on the server side. It takes the files and saves them to the upload folder

@app.route("/upload", methods=["POST"])
def upload():
    uploaded_files = flask.request.files.getlist("dicom")
    print(uploaded_files, file=sys.stderr)
    for file in uploaded_files:
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

It's more than likely bad code on my part, but I have things set so that the server starts processing the images once they are uploaded. So I'd like to also upload the recipient email address in the same request. Any help would be greatly appreciated!

CodePudding user response:

Client

test_files = {('dicom', open("Pre_1", "rb")),
              ('dicom', open("Pre_2", "rb")),
              ('dicom', open("Post_1", "rb")),
              ('dicom', open("Post_2", "rb"))}
payload = {'email': 'myemail'}
response = requests.post("mywebsite", files = test_files, data = payload)

Server

@app.route("/upload", methods=["POST"])
def upload():
    uploaded_files = flask.request.files.getlist("dicom")
    payload = request.form['email']

Works great now

  •  Tags:  
  • Related