def upload_file(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): instance = FileUpload(uploaded_by=request.user, file_type=FileUpload.SYLLABUS, file_field=request.FILES['file']) instance.save() return HttpResponse(instance.file_field.read()) else: return HttpResponse("hahahahhahaha") else: form = UploadFileForm() return render_to_response('repository/upload.html', {'form': form}, context_instance=RequestContext(request))
def index_view(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) #form if form.is_valid(): #save the file into the database fileupload = FileUpload(filename=request.FILES['filename'], email=request.POST['email']) fileupload.save() # html = '<html><body>The form was valid. Form was %s. Filename was %s (model). finally, in the post request. Request.FILES[\'filename\'] was %s </body></html>' % (form, filename, request.FILES['filename']) return redirect('/handlefile') return HttpResponse("<html><body>Form was not valid</body></html>") else: form = UploadFileForm() #empty, unbound form files = FileUpload.objects.all() return render(request, 'index.html', {'form': form})
def create_file(self, user): ''' Saves uploaded file ''' file_name = os.path.basename( self.request.files['file_data'][0]['filename']) char_white_list = ascii_letters + digits + "-._" file_name = filter(lambda char: char in char_white_list, file_name) content = guess_type(file_name) if content[0] is None: self.render("share_upload/share_files.html", errors=["Unknown file content, please zip and upload"], shares=user.team.files) elif len(file_name) < 1: self.render("share_upload/share_files.html", errors=["Invalid file name"]) else: uuid = unicode(uuid4()) filePath = self.application.settings['shares_dir'] + '/' + uuid save = open(filePath, 'w') data = b64encode(self.request.files['file_data'][0]['body']) save.write(data) save.close() file_upload = FileUpload( file_name=unicode(file_name), content=unicode(str(content[0])), uuid=uuid, description=unicode( self.get_argument('description', 'No description.')), byte_size=len(self.request.files['file_data'][0]['body']), team_id=user.team.id) dbsession.add(file_upload) dbsession.flush() return file_upload
def register_upload(request, name, checksum, size, target_dir): upload_dir = getattr(settings, "UPLOAD_DIR", None) if upload_dir is not None: target_dir = os.path.join(upload_dir, target_dir) if not target_dir.startswith(upload_dir): raise RuntimeError("Target directory (%s) is outside upload dir: %s" % (target_dir, upload_dir)) upload = FileUpload() upload.owner = request.user upload.name = name upload.checksum = checksum.lower() upload.size = size upload.target_dir = target_dir upload.save() return (upload.id, upload.upload_key)
def upload_files(request): if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): print "form is valid" form_data = form.cleaned_data saved_file = handle_uploads(form_data['uploaded_file'], form_data['filename']) newdoc = FileUpload() newdoc.uploaded_file_url = saved_file newdoc.description = form_data['description'] newdoc.filename = form_data['filename'] newdoc.save() newdoc.allowed_users = form_data['allowed_users'] newdoc.save() messages.add_message(request, messages.INFO, "File "+newdoc.filename+" Uploaded Sucessfully") return HttpResponseRedirect(reverse('uploaded_files')) form = UploadForm() base_template = 'dashboard.html' return render(request,'fileupload/upload_file.html',{'form':form, 'base_template':base_template })
def download(request): """Saves image from URL and returns ID for use with AJAX script""" if not request.user.is_staff: raise Http404 if request.method == "GET": f = FileUpload(); f.title = request.GET["title"] or "untitled" f.description = request.GET["description"] url = urllib.unquote(request.GET["photo"]) file_content = urllib.urlopen(url).read() file_name = url.split("/")[-1] f.save_upload_file(file_name, file_content) f.save() return HttpResponse("%s" % (f.id)) else: raise Http404
def get(self, *args, **kwargs): try: uuid = self.get_argument('uuid') except: self.render("user/error.html", operation = "File Download", errors = "Missing parameter") user = self.get_current_user() share = FileUpload.by_uuid(uuid) if share == None or share.team_id != user.team_id: self.render("user/error.html", operation = "File Download", errors = "File does not exist") else: upload = open(self.application.settings['shares_dir']+'/'+share.uuid, 'r') data = upload.read() upload.close() self.set_header('Content-Type', share.content) self.set_header('Content-Length', share.byte_size) self.set_header('Content-Disposition', 'attachment; filename=%s' % share.file_name) self.write(b64decode(data)) self.finish()
def get(self, *args, **kwargs): """ Get a file and send it to the user """ uuid = self.get_argument("uuid", default="", strip=True) user = self.get_current_user() share = FileUpload.by_uuid(uuid) if share is None or share.team_id != user.team_id: self.render("share_upload/share_error.html") else: upload = open(self.application.settings["shares_dir"] + "/" + share.uuid, "r") data = upload.read() self.set_header("Content-Type", share.content) self.set_header("Content-Length", share.byte_size) self.set_header( "Content-Disposition", "attachment; filename=%s" % share.file_name, # Chars whitelisted by FileUpload object ) self.write(b64decode(data)) upload.close() self.finish()
def get(self, *args, **kwargs): ''' Get a file and send it to the user ''' uuid = self.get_argument('uuid', default="", strip=True) user = self.get_current_user() share = FileUpload.by_uuid(uuid) if share is None or share.team_id != user.team_id: self.render("share_upload/share_error.html") else: upload = open(self.application.settings['shares_dir'] + '/' + share.uuid, 'r' ) data = upload.read() self.set_header('Content-Type', share.content) self.set_header('Content-Length', share.byte_size) self.set_header('Content-Disposition', 'attachment; filename=%s' % share.file_name # Chars whitelisted by FileUpload object ) self.write(b64decode(data)) upload.close() self.finish()