2016-07-30 31 views
0

Ich habe ein Formular in HTML/PHP erstellt und Flasher für Back-End verwendet.Datei/Bild kann nicht in flaskapp hochgeladen werden

Alles funktioniert perfekt, außer wenn ich Bild hochzuladen versuche ich bin immer wieder den Fehler zu sehen:

error is**"UnboundLocalError: local variable 'filename' referenced before assignment"** 

mein flaskapp Code-Schnipsel ist

@app.route('/apple', methods=['GET', 'POST']) 
def apple(): 
    onlineAppForm = RegForm() 
    if request.method == 'POST': 
    try: 
     file = request.files['file'] 
     if file and allowed_file(file.filename): 
      filename = secure_filename(file.filename) 
      file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) 
    except Exception as e: 
     print "Form without file "+str(e) 
    return render_template("apply2.html", data=data, filename=filename, form=onlineAppForm) 

dieser Ordner mein Upload ist

UPLOAD_FOLDER = 'static/uploads' 
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) 

app = Flask(__name__) 
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 
app.secret_key = os.urandom(24) 

Ich bekomme nicht, wo der Fehler ist.

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

das ist meine Form

<tr> 
    <td class="sectionheading">Upload Scanned Copies</td> 
</tr> 
<tr> 
    <td height="12">&nbsp;</td> 
</tr> 
<tr id="photosignthumb_span" height="40"> 
    <td align="left" valign="middle" width="100%">&nbsp;<b> 
     <span class="mandatory">*</span> 
     Please upload scanned copies of your photo </b> 
     <input type="file" name="photo" id="photofile" 
     method="post"/><br> 
     Please upload your recent passport size photograph:max 
     80KB(Only JPEG and JPG formats) 
    </td> 
</tr> 
<tr id="photosignthumb_span" height="40"> 
    <td align="left" valign="middle" width="100%">&nbsp;<b> 
     <span class="mandatory">*</span> 
     Please upload scanned copies of your signature </b> 
     <input type="file" name="sign" id="signfile" 
     method="post"/><br> 
     Please upload your recent passport size photograph:max 
     80KB(Only JPEG and JPG formats) 
    </td> 
</tr> 
+0

Was passiert, wenn 'if file und allowed_file (file.filename)' nicht besteht oder eine * exception * ausgelöst wird? ... 'filename' ist offensichtlich nicht vorhanden! –

+0

ja wahr, dass m bekommen, m nicht in der Lage zu laden, so ja Dateiname wird nicht existieren, aber was muss ich tun, um zu übergeben. –

+0

Können Sie Code von 'allowed_file' und relevante Vorlagendatei (von der Sie Ihre Dateien hochladen) eingeben? –

Antwort

0

filename Existenz auf einigen Bedingungen abhängig:

1 - file = request.files['file']
2-if file and allowed_file(file.filename):
3 - Keine Ausnahme angehoben wird.

Also, wenn eine der oben genannten nicht passiert, dann filename wird nicht ins Leben und daher Ihre Fehlermeldung.

EDIT:

Zitiert aus flaskdocs auf Datei-Upload:

  1. A tag is marked with enctype=multipart/form-data and an is placed in that form.
  2. The application accesses the file from the files dictionary on the request object.
  3. Use the save() method of the file to save the file permanently somewhere on the filesystem.

in Form der Suche Ihr vorausgesetzt, es scheint, Sie haben keinen form Block, sollten Sie so etwas wie haben:

<form action="/apple" method="post" enctype="multipart/form-data"> 
<tr> 
    <td class="sectionheading">Upload Scanned Copies</td> 
</tr> 
<tr> 
    <td height="12">&nbsp;</td> 
</tr> 
<tr id="photosignthumb_span" height="40"> 
    <td align="left" valign="middle" width="100%">&nbsp;<b> 
     <span class="mandatory">*</span> 
     Please upload scanned copies of your photo </b> 
     <input type="file" name="photo" id="photofile" 
     method="post"/><br> 
     Please upload your recent passport size photograph:max 
     80KB(Only JPEG and JPG formats) 
    </td> 
</tr> 
<tr id="photosignthumb_span" height="40"> 
    <td align="left" valign="middle" width="100%">&nbsp;<b> 
     <span class="mandatory">*</span> 
     Please upload scanned copies of your signature </b> 
     <input type="file" name="sign" id="signfile" 
     method="post"/><br> 
     Please upload your recent passport size photograph:max 
     80KB(Only JPEG and JPG formats) 
    </td> 
</tr> 
</form> 
+0

können Sie erneut überprüfen. Ich habe meine Frage aktualisiert –

+0

@ SumitOjha..Check aktualisierte Antwort .. –