title: How to handle file upload asynchronously in pyramid
body: <p>I'm writing a web application in python/pyramid that handles the upload of a file (via <a href="http://blueimp.github.io/jQuery-File-Upload/" rel="nofollow">jquery uploader</a>). The code for the upload is:</p>
  
  <pre><code>@view_config(route_name='file.upload', renderer='json')
  def file_upload(request):
  
  for item, filestorage in request.POST.items():
      f = File.create(filestorage)
  
      u = Upload.create(f.hash)
  
  return {
      'url': request.route_url('file.get', uploadid=u.urlid)
  }
  </code></pre>
  
  <p>where then File.create makes a sha1 of the file and moves it in a permanent location while putting the metadata in a database. (File is a SQLAlchemy class actually)</p>
  
  <p>The problem here is that the view callable is called <strong>after</strong> that the file transfer to the server is complete. This poses two problems: the first one is that i'm unable to reject the file transfer if it's bigger than some size. The second is that i have to wait to receive the whole file and then i can start hashing it.</p>
  
  <p>What i would like to obtain is to start process the file - some sort of stream - as soon i'm getting the data so i can hash it while the user is uploading it and i can stop the transfer if the size is bigger than some value.</p>