本文整理汇总了Python中django.core.files.uploadedfile.UploadedFile.read方法的典型用法代码示例。如果您正苦于以下问题:Python UploadedFile.read方法的具体用法?Python UploadedFile.read怎么用?Python UploadedFile.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.files.uploadedfile.UploadedFile
的用法示例。
在下文中一共展示了UploadedFile.read方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_file
# 需要导入模块: from django.core.files.uploadedfile import UploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.UploadedFile import read [as 别名]
def parse_file(self, file: UploadedFile, _) -> Iterable[Article]:
data = file.read()
try:
for para in self.split_file(data):
yield self.parse_document(para)
except ApaError:
log.error("APA parse attempt failed.")
if settings.DEBUG:
log.error("The generated HTML can be found in /tmp/apa_unrtf.html")
raise
示例2: upload
# 需要导入模块: from django.core.files.uploadedfile import UploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.UploadedFile import read [as 别名]
def upload(req):
if req.method == 'POST':
if 'file' in req.FILES:
file = req.FILES['file']
filename = file._name
image = UploadedFile.read(file)
print image
# apps.ImageTrans.getGrayscale(file)
image2 = cv2.imread(image,0)
print image2
fp = open('%s/%s' % ('image/data', filename) , 'wb')
for chunk in file.chunks():
fp.write(chunk)
fp.close()
return HttpResponse('File Uploaded')
return HttpResponse('Failed to Upload File')
示例3: fileupload
# 需要导入模块: from django.core.files.uploadedfile import UploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.UploadedFile import read [as 别名]
def fileupload(request):
if request.method == 'POST':
cc = request.POST.get('cc');
myexcel = request.FILES['files[]']
excel_obj = UploadedFile(myexcel)
workbook = xlrd.open_workbook(file_contents = excel_obj.read())
all_worksheets = workbook.sheet_names()
worksheet_name = all_worksheets[0]
worksheet = workbook.sheet_by_name(worksheet_name)
for rownum in xrange(worksheet.nrows):
tmp = []
for entry in worksheet.row_values(rownum):
tmp.append(entry)
print tmp
return JsonResponse({'status': 'fileupload_ok'})
示例4: save_document
# 需要导入模块: from django.core.files.uploadedfile import UploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.UploadedFile import read [as 别名]
def save_document(request_file, content_subdir, related_obj, ashash = True):
uploadedfile = UploadedFile(request_file)
file_content = uploadedfile.read()
doc_obj = Document()
doc_obj.filehash = md5(file_content).hexdigest()
doc_obj.urlencfilename = quote(uploadedfile.name)
doc_obj.filename = uploadedfile.name
doc_obj.content_type = uploadedfile.file.content_type
if ashash:
doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filehash
else:
doc_obj.filepath = settings.BASE_DIR + content_subdir + doc_obj.filename
if related_obj.__class__.__name__.lower() == "queryset":
if len(related_obj) == 1:
setattr(doc_obj, related_obj[0].__class__.__name__.lower(), related_obj[0])
else:
print "ERROR: The queryset object had %s elements to it" % str(len(related_obj))
else:
setattr(doc_obj, related_obj.__class__.__name__.lower(), related_obj)
doc_obj.save()
wfile = open(doc_obj.filepath, "w")
wfile.write(file_content)
wfile.close()
示例5: _read
# 需要导入模块: from django.core.files.uploadedfile import UploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.UploadedFile import read [as 别名]
def _read(file: UploadedFile, n=None):
"""Read the file, guessing encoding if needed"""
binary_content = file.read(n)
encoding = _get_encoding(file.encoding, binary_content)
return binary_content.decode(encoding)
示例6: multiuploader
# 需要导入模块: from django.core.files.uploadedfile import UploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.UploadedFile import read [as 别名]
def multiuploader(request, patient_id=None):
"""
Main Multiuploader module.
Parses data from jQuery plugin and makes database changes.
"""
if request.method == 'POST':
log.info('received POST to main multiuploader view')
if request.FILES == None:
return HttpResponseBadRequest('Must have files attached!')
#getting file data for farther manipulations
file = request.FILES[u'files[]']
wrapped_file = UploadedFile(file)
filename = wrapped_file.name
file_size = wrapped_file.file.size
log.info ('Got file: "%s"' % str(filename))
log.info('Content type: "$s" % file.content_type')
#writing file manually into model
#because we don't need form of any type.
print request.POST
if patient_id=='new':
print wrapped_file.read()
return HttpResponse(status=204)
image = MultiuploaderImage()
patient=Patient.objects.get(pk=patient_id)
image.filename=str(filename)
image.image=file
image.key_data = image.key_generate
patient.multiuploaderimage_set.add(image)
log.info('File saving done')
#getting thumbnail url using sorl-thumbnail
if 'image' in file.content_type.lower():
im = get_thumbnail(image, "80x80", quality=50)
thumb_url = im.url
else:
thumb_url = ''
#settings imports
try:
file_delete_url = settings.MULTI_FILE_DELETE_URL+'/'
file_url = settings.MULTI_IMAGE_URL+'/'+image.key_data+'/'
except AttributeError:
file_delete_url = 'multi_delete/'
file_url = 'multi_image/'+image.key_data+'/'
"""
is actually: [{"name": "Screenshot from 2012-11-14 16:17:46.png", "url": "multi_image/95925526541943247735000327303075602114185579370918344597903504067450818566531/", "thumbnail_url": "/media/cache/f8/bd/f8bd83aadeba651ff9c040bb394ce117.jpg", "delete_type": "POST", "delete_url": "multi_delete/7/", "size": 38520}]
should be: {"files":[{"url":"http://jquerey-file-upload.appspot.com/AMIfv9734HSTDGd3tIybbnKVru--IjhjULKvNcIGUL2lvfqA93RNCAizDbvP-RQJNbh-N9m8UXsk-90jFFYSp8TlbZYhEcNN6Vb9HzQVQtdmF83H6sE_XkdnlI2V8lHX5V3Y4AamdX6VMbAt9sNWNx2BVGzhTfAYkRLYmRE1VzzWSe9C8c8Fu8g/Screenshot%20from%202012-11-14%2016%3A17%3A46.png","thumbnail_url":"http://lh5.ggpht.com/fcjVNT6qUGoMDtqqaNDNtU4mghy34qlzfj2GujikLgC7Nj5Bs4LUT_DWG_Q8OWujqvYHsKbeQ9pkvoAW4WiaubmqQxobIPyt=s80","name":"Screenshot from 2012-11-14 16:17:46.png","type":"image/png","size":38520,"delete_url":"http://jquery-file-upload.appspot.com/AMIfv9734HSTDGd3tIybbnKVru--IjhjULKvNcIGUL2lvfqA93RNCAizDbvP-RQJNbh-N9m8UXsk-90jFFYSp8TlbZYhEcNN6Vb9HzQVQtdmF83H6sE_XkdnlI2V8lHX5V3Y4AamdX6VMbAt9sNWNx2BVGzhTfAYkRLYmRE1VzzWSe9C8c8Fu8g/Screenshot%20from%202012-11-14%2016%3A17%3A46.png?delete=true","delete_type":"DELETE"}]}
"""
#generating json response array
result = {
'files': [ {"name":filename,
"size":file_size,
"url":file_url,
"thumbnail_url":thumb_url,
"delete_url":file_delete_url+str(image.pk)+'/',
"delete_type":"POST",}
]
}
response_data = simplejson.dumps(result)
#checking for json data type
#big thanks to Guy Shapiro
if "application/json" in request.META['HTTP_ACCEPT_ENCODING']:
mimetype = 'application/json'
else:
mimetype = 'text/plain'
return HttpResponse(status=204)
#response_data, mimetype=mimetype)
else: #GETim
return HttpResponse('Only POST accepted')
示例7: accept_uploaded_photo
# 需要导入模块: from django.core.files.uploadedfile import UploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.UploadedFile import read [as 别名]
def accept_uploaded_photo(request, album_id):
"""
Main Multiuploader module.
Parses data from jQuery plugin and makes database changes.
"""
if request.method == 'POST':
logid = random.randint(0,1000)
log.info('[%s] received POST to main multiuploader view' % logid)
if request.FILES == None:
return HttpResponseBadRequest('Must have files attached!')
#getting file data for farther manipulations
file = request.FILES[u'files[]']
wrapped_file = UploadedFile(file)
filename = wrapped_file.name
file_size = wrapped_file.file.size
log.info ('[%s] Got file: "%s"' % (logid, str(filename)))
# Write out file to disk as a temp file
randnumber = logid # use the random number here too
temp_filename = '%stmp%s_%s' % (settings.TEMP_DIRECTORY,randnumber, filename)
log.info('[%s] Writing out to: %s' % (logid, temp_filename))
destination = open(temp_filename, 'wb+')
if wrapped_file.multiple_chunks():
for chunk in wrapped_file.chunks():
destination.write(chunk)
else:
destination.write(wrapped_file.read())
destination.close()
# Dump out EXIF Tags
# im = Image.open(temp_filename)
# if hasattr( im, '_getexif' ):
# exifinfo = im._getexif()
# if exifinfo:
# for tag, value in exifinfo.items():
# decoded = ExifTags.TAGS.get(tag, tag)
# log.info('Found tag: %s, value: %s' % (decoded,value))
orientation = None
date_taken = None
# Make full size and thumbsize
try:
im = Image.open(temp_filename)
except IOError:
log.info('[%s] Error opening file %s: %s %s' % (logid, temp_filename, e.errno, e))
return HttpResponseBadRequest('Could not read file')
if hasattr( im, '_getexif' ):
exifinfo = im._getexif()
if exifinfo:
for tag, value in exifinfo.items():
decoded = ExifTags.TAGS.get(tag, tag)
# if decoded != 'MakerNote':
# if decoded != 'UserComment':
# log.info('Found tag: %s, value: %s' % (decoded,value))
if decoded == 'Orientation':
orientation = value
log.info('[%s] Found tag: %s, value: %s' % (logid,decoded,value))
elif decoded == 'DateTime':
date_taken = datetime.strptime(value, "%Y:%m:%d %H:%M:%S")
log.info('[%s] Found tag: %s, value: %s, date_taken=%s' % (logid,decoded,value,date_taken))
# We rotate regarding to the EXIF orientation information
if orientation:
if orientation == 1:
# Nothing
log.info('[%s] Orientation: No rotation necessary' % logid)
pass
elif orientation == 2:
# Vertical Mirror
log.info('[%s] Orientation: Vertical flip' % logid)
im = im.transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 3:
# Rotation 180
log.info('[%s] Orientation: Rotation 180' % logid)
im = im.transpose(Image.ROTATE_180)
elif orientation == 4:
# Horizontal Mirror
log.info('[%s] Orientation: Horizontal Mirror' % logid)
im = im.transpose(Image.FLIP_TOP_BOTTOM)
elif orientation == 5:
# Horizontal Mirror + Rotation 270
log.info('[%s] Orientation: Flip top bottom, rot 270' % logid)
im = im.transpose(Image.FLIP_TOP_BOTTOM).transpose(Image.ROTATE_270)
elif orientation == 6:
# Rotation 270
log.info('[%s] Orientation: Rotate 270' % logid)
im = im.transpose(Image.ROTATE_270)
elif orientation == 7:
# Vertical Mirror + Rotation 270
log.info('[%s] Orientation: Flip left right, rotate 270' % logid)
im = im.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_270)
elif orientation == 8:
# Rotation 90
log.info('[%s] Orientation: Rotate 90' % logid)
im = im.transpose(Image.ROTATE_90)
#------------------
# Save the transposed image to disk
#.........这里部分代码省略.........