Flask request.get.args - one arg is missing

I'm sending a POST request from my website with some FormData:
  var formData = new FormData();
  formData.append('file',file);
  formData.append('timestamp', window.performance.now())
  console.log(formData)
  console.log(window.performance.now())
  var xhr = new XMLHttpRequest();
  xhr.addEventListener("load", () => {
    console.log("asdf");
  });
  xhr.open("POST", "http://flaskmin.run.aws-usw02-pr.ice.predix.io/receivedoc");
  xhr.send(formData);
Where file is an actual Excel file.
I'm handling it with a Flask server:
@app.route('/receivedoc', methods=['POST', 'GET', 'OPTIONS'])
@crossdomain(origin='*')
def upload_file():
    if request.method == 'POST':
        print(request.files)
        print("Timestamp" + request.args.get('timestamp'))
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        #Do stuff with the file
Curiously, the server DOES receive the file, but not the timestamp. When I try to print it, I get the error TypeError: must be str, not NoneType. When I do console.log(window.performance.now()) on the website, I DO see the time on the Chrome dev tools console, but it won't get to the Flask server, stays None. What am I missing? I've tried formData.append('timestamp', String(window.performance.now())), too, didn't help.
UPDATE
So I added some logging, and there's some weird stuff going on. I'm doing:
   fileUpload: function(file) {
      console.log("fileUpload function called")     
      var formData = new FormData();
      formData.append('file',file);
      formData.append('t_stamp123', 'test123')
      console.log(formData)
      console.log(formData.files)
      console.log(formData.file)
      console.log(file)
      var xhr = new XMLHttpRequest();
      xhr.open("POST", "http://localhost:7733/receivedoc");
      xhr.send(formData);
      var xhr1 = new XMLHttpRequest();
This gets called via:
  self.fileUpload(uploadComponentId.files[0])
And uploadComponentId is kind of a <form> element. So the weird thing is my console for this shows:
fileUpload function called
pacing-view.html:60 FormData {}
pacing-view.html:61 undefined
pacing-view.html:62 undefined
pacing-view.html:63 File(114688) {name: "GL_Details_03212018_094512.xls", lastModified: 1521640039000, lastModifiedDate: Wed Mar 21 2018 14:47:19 GMT+0100 (CET), webkitRelativePath: "", size: 114688, …}
And yet, the server DOES get the file and I CAN write it to the disk (I'm running everything on localhost for now)!!! Even though formData seems empty, the file gets through to the server, but the timestamp doesn't? Wow.
UPDATE 2
When I do remove the formData.append('file',file); line, the file does net get sent to the server, and I get an UnboundLocalError: local variable 'filename' referenced before assignment. So I am definitely not sending the file somewhere else in the code, it must be with formData.

Комментарии

Популярные сообщения из этого блога

Skipping acquire of configured file 'contrib/binary-i386/Packages' as repository … doesn't support architecture 'i386'

Connection string for MariaDB using ODBC

Celery like system based on django channels