0

I'm tring to upload two files to server but the try except where I wrote in @app.route('result') is always in exception

from flask import Flask,render_template,redirect, url_for,request,redirect
import os, sys
from pymongo import MongoClient
from werkzeug import secure_filename
app = Flask(__name__,static_folder='static', static_url_path='')
import numpy
@app.route('/')
def showRoot():
    return render_template('index.html')

@app.route('/result/')
def result():

    return render_template('test.html')

ALLOWED_EXTENSIONS = set(['txt', 'docx', 'png', 'jpg'])
def allowed_file(filename):

    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/request_page/', methods=['GET','POST'])
def request_page():
    UPLOAD_FOLDER = '/var/www/helloworldapp/app/uploads/'
    UPLOAD_FOLDER = os.path.expanduser(UPLOAD_FOLDER)
    if (not os.path.exists(UPLOAD_FOLDER)):
       os.makedirs(UPLOAD_FOLDER)
    app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
    try:
        Case_ID = '171129001'
        mRNA_file = request.files['mRNA_file_name']
        lncRNA_file = request.files['lncRNA_file_name']
        if (mRNA_file != '' and allowed_file(mRNA_file.filename)) and     (lncRNA_file != '' and allowed_file(lncRNA_file.filename)):
               mRNA_file_name = Case_ID+secure_filename(mRNA_file.filename)
               lncRNA_file_name = Case_ID+secure_filename(lncRNA_file.filename)
               mRNA_file.save(UPLOAD_FOLDER+ mRNA_file_name)
               lncRNA_file.save(UPLOAD_FOLDER + lncRNA_file_name)

               #import sys
               #sys.path.append('/var/www/helloworldapp/app')
               #from . import Expression_profiles_preprocessing
                                                  #return(Expression_profiles_preprocessing.Concatenating_gene_expression_profile(Case_ID,mRNA_file_name,lncRNA_file_name))

         else:
             return 'Upload Failed'
     except Exception as e:
          return render_template('test.html', error=str(e))

the except result on my website is like this web result

Is there any solution to solve this problem? By the way , two files that upload by html can be seen in my server's document.

1 Answers1

0

Im going to go out on a limb here without the call stack and say that your issue might be solved by changing this:

mRNA_file = request.files.get('mRNA_file_name')
lncRNA_file = request.files.get('lncRNA_file_name')

Ive run into this issue quite a few times in Flask, and 80% of the time this solved the issue.

Edit:

Your issue is that by default, .get returns None. Therefore, since you are only checking if mRNA_filename or lncRNA_filename does NOT equal an empty string, then this conditional statement is passing. What I would do instead is this:

mRNA_file = request.files.get('mRNA_file_name') <-- Here you can add another argument that would act as the default, else this will return None
lncRNA_file = request.files.get('lncRNA_file_name')
        if (mRNA_file and allowed_file(mRNA_file.filename)) and     (lncRNA_file and allowed_file(lncRNA_file.filename)):
               mRNA_file_name = Case_ID+secure_filename(mRNA_file.filename)
               lncRNA_file_name = Case_ID+secure_filename(lncRNA_file.filename)
               mRNA_file.save(UPLOAD_FOLDER+ mRNA_file_name)
               lncRNA_file.save(UPLOAD_FOLDER + lncRNA_file_name)
Andrew Graham-Yooll
  • 2,148
  • 4
  • 24
  • 49
  • Do you have the website about how to use request.file.get() function When I change my code into this one it has another Attribute Error when the code executed to line.33 It's about {'NoneType' object has no attribute 'filename'} – st504132005 Jan 01 '18 at 12:17
  • That is because the get operation is returning None. Instead of checking the equality of mRNA_file and lncrna_file to != ' ', set it to just if mRNA_file and lncrna_file. This will yield false if either one of those two variables is false. The core of your problem is that the files with keys mRNA_file or lncRNA_file don't exist. – Andrew Graham-Yooll Jan 04 '18 at 11:01
  • @st504132005 See my edited answer for more info and suggestions – Andrew Graham-Yooll Jan 04 '18 at 21:51