Quiz

  1. For question 4, I am unsure what file type is referring to exactly. The reading specifies 'file contents' as a file upload security concern. Is this interchangeable with file type?

    Pretty much. When we say something is a JPEG, we are referring to the file type and also its contents.

  2. Can we review MIME types and how they're relevant / used in the file upload process?

    Sure. MIME types are an agreed-upon way to label or categorized file types. For example:

    • text/html
    • text/javascript
    • image/jpeg
    • image/png
    • application/pdf
    • many others
  3. How would we actually display an image from a saved file?

    Great question!

    • We will have a route that will
    • have a URL that somehow identifies the picture that is wanted, such as /profile/image or /pic/
    • the handler determines the corresponding filename (probably accessing the database to do so)
    • sends the image to the browser using Flask's send_from_directory() function.

    We'll see an example today.

  4. I'm also a little confused about pathnames

    The pathname is where the file is stored. For example, it might be stored in a local file: uploads/123.jpg

    Or it might be in some shared folder: /students/teamacct/uploads/123.jpg.

    The latter directory would have to be world-writable, so that all the team members could write to it, but that's easily done.

    The latter technique can work well for uploads to the team project, but I've also had lots of students develop locally and later transfer the code and files to the team account.

  5. Is there a way to display an uploaded image from our database on a webpage as a clickable link using Flask, so that clicking on it redirects the user to a new page or action?

    Sure. We can wrap a picture with a button or hyperlink, both of which are clickable elements:

    
    
    <a href="{{url_for('new_page')}}"> <img src="{{url_for('pic_file', p.nm=123)}}"> {{p.name}} </a>
    
    <form action="{{url_for('new_page')}}"
       <button> <img src="{{url_for('pic_file', p.nm=123)}}"> {{p.name}} </button>
    </form>
    
    
    
  6. could we go over the upload route again? / Can you go over the app.py code again?

    Sure. let's do that. example code

  7. how to determine what size limit is the best? what is the convention.

    Depends. For lots of purposes, such as thumbnails on a photo gallery or a profile picture, a modest size picture is fine. Maybe a quarter of a megabyte or so.

    For some sites, you'll want to allow people to upload high-res photos.

    If you're allowing people to upload PDFs for something, those might be quite large.

    Create some sample files and look at their sizes.