title: Send image file from html to Flask and save it in my system
body: <p>I am developing a webpage on uploading blogs with image cover. I want to upload image files from HTML page to flask and save it in my system. I have tried the code to do it but everytime I run the code , it shows me 500 Internal server error. Can anyone help me with that.</p>
  <p>This is my html file - addblog.html</p>
  <pre><code>  &lt;form action=&quot;/addblog&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
  
      &lt;div&gt;
        &lt;label for=&quot;image_uploads&quot; class=&quot;btn btn-primary&quot;&gt;Upload Cover Image (PNG, JPG)&lt;/label&gt;
        &lt;input type=&quot;file&quot; id=&quot;image_uploads&quot; name=&quot;image_uploads&quot; accept=&quot;.jpg, .jpeg, .png&quot;&gt;
      &lt;/div&gt;
      &lt;div class=&quot;preview&quot;&gt;
        &lt;p&gt;No files currently selected for upload&lt;/p&gt;
      &lt;/div&gt;&lt;br&gt;
  
      &lt;div class=&quot;form-group&quot;&gt;
        &lt;label for=&quot;exampleFormControlTextarea1&quot;&gt;Content&lt;/label&gt;
        &lt;textarea class=&quot;form-control&quot; id=&quot;exampleFormControlTextarea1&quot; name=&quot;content&quot; rows=&quot;10&quot; cols=&quot;80&quot;&gt;&lt;/textarea&gt;
      &lt;/div&gt;
  
      &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Upload&lt;/button&gt;
    &lt;/form&gt;
  </code></pre>
  <p>This is my flask file.</p>
  <pre><code>    from flask import Flask, session, render_template, request, redirect
      from flask_session import Session
      from cs50 import SQL
      from tempfile import mkdtemp
      from werkzeug.security import check_password_hash, generate_password_hash
      from werkzeug.utils import secure_filename
  
  
      from helpers import login_required, convertToBinaryData, insertBLOB
  
      import sqlite3
      import os
  
  
      app = Flask(__name__)
  
  
      app.config[&quot;IMAGE_UPLOADS&quot;] = &quot;/home/ubuntu/FINAL_PROJECT/Blog_webapp/static/images/uploads&quot;
  
      # Configure session to use filesystem (instead of signed cookies)
      app.config[&quot;SESSION_FILE_DIR&quot;] = mkdtemp()
      app.config[&quot;SESSION_PERMANENT&quot;] = False
      app.config[&quot;SESSION_TYPE&quot;] = &quot;filesystem&quot;
      Session(app)
  
      # Configure CS50 Library to use SQLite database
      db = SQL(&quot;sqlite:///blog.db&quot;)
  
      @app.route(&quot;/addblog&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])
      @login_required
      def add():
          if request.method == &quot;GET&quot;:
               return render_template(&quot;addblog.html&quot;)
          else:
               image2 = request.files[&quot;image_uploads&quot;]
               image2.save(os.path.join(app.config[&quot;IMAGE_UPLOADS&quot;], image2.filename))
               content = request.form.get(&quot;content&quot;)
  
               print(image,content)
               insertBLOB(session[&quot;user_id&quot;], image, content)
  
               return render_template(&quot;addblog.html&quot;)
  </code></pre>
  <p>This is the snippet of my terminal.<a href="https://i.stack.imgur.com/cF5sE.png" rel="nofollow noreferrer">enter image description here</a></p>