Python的两种上传图片方式
1.上传至七牛云服务器
2.上传至自己服务器
1.上传至七牛云服务器
access_key = '替换成你的'
secret_key = '替换成你的'
bucket_name = '替换成你的'
url = '替换成你的'
q = qiniu.Auth(access_key, secret_key)
def qiniu_upload(key, localfile):
token = q.upload_token(bucket_name, key, 3600)
ret, info = qiniu.put_file(token, key, localfile)
if ret:
return 'http://{0}/{1}'.format(url, ret['key'])
else:
raise Exception('上传失败,请重试')
@csrf_exempt
def upload_qiniu(request):
"""
@api {POST} /upload_qiniu/ [上传图片至七牛]
* @apiVersion 0.0.1
* @apiGroup upload
@apiParamExample {params} 请求参数
"image":"" "图片文件"
@apiSuccessExample {json} 成功返回
{
"message": "",
"next": "",
"data": "",
"response": "ok",
"error": ""
}
@apiSuccessExample {json} 失败返回
{
"message": "",
"next": "",
"data": null,
"response": "fail",
"error": "上传失败","缺少参数"
}
"""
image = request.FILES.get("image")
if not image:
return ajax.jsonp_fail(request, u"缺少参数")
service_name = save_block_file(image)
data=qiniu_upload(service_name,get_absolute_file_path(service_name))
if data:
return ajax.jsonp_ok(request, data)
else:
return ajax.jsonp_fail(request, u"上传失败")
def save_upload_file(new_file_path, raw_file,name):
"""
功能说明:保存上传文件
raw_file:原始文件对象
new_file_path:新文件绝对路径
"""
try:
if os.path.exists(new_file_path):
try:
os.remove(new_file_path)
except:
pass
content = raw_file.read()
fp = open(new_file_path, 'wb')
fp.write(content)
fp.close()
return name
except Exception as e:
print (e)
return False
def save_block_file(block_file):
"""
:param block_file: 文件对象
:return:
"""
now_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
name = '%s%s' % (now_time, block_file.name)
block_file_path = get_absolute_file_path(name).replace("\\", "/")
return save_upload_file(block_file_path, block_file, name)
def get_absolute_file_path(file_name):
"""
功能说明:返回绝对路径字符串
file_name:文件名字
"""
media_root = settings.UPLOAD
print ("media_root",media_root)
absolute_file_path = os.path.join(media_root, file_name)
print("absolute_file_path", absolute_file_path)
file_dir = os.path.dirname(absolute_file_path)
print ("file_dir", file_dir)
if not os.path.exists(file_dir):
os.makedirs(file_dir)
return absolute_file_path

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
上传至自己服务器
@csrf_exempt
def insert_img(request):
"""
@api {POST} /upload/ [上传图片]
* @apiVersion 0.0.1
* @apiGroup note
@apiParamExample {params} 请求参数
"image":"" "图片文件"
"""
image = request.FILES.get("image")
if not image:
return ajax.jsonp_fail(request, u"缺少参数")
service_name = save_block_file(image)
path = '%s/%s' % ("你的服务器地址", service_name)
if not path:
return ajax.jsonp_fail(request, u"上传失败")
else:
return ajax.jsonp_ok(request, path)
def save_upload_file(new_file_path, raw_file,name):
"""
功能说明:保存上传文件
raw_file:原始文件对象
new_file_path:新文件绝对路径
"""
try:
if os.path.exists(new_file_path):
try:
os.remove(new_file_path)
except:
pass
content = raw_file.read()
fp = open(new_file_path, 'wb')
fp.write(content)
fp.close()
return name
except Exception as e:
print(e)
return False
def save_block_file(block_file):
"""
:param block_file: 文件对象
:return:
"""
now_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
name = '%s%s' % (now_time, block_file.name)
block_file_path = get_absolute_file_path(name).replace("\\", "/")
return save_upload_file(block_file_path, block_file,name)
def get_absolute_file_path(file_name):
"""
功能说明:返回绝对路径字符串
file_name:文件名字
"""
media_root = settings.UPLOAD
print("media_root",media_root)
absolute_file_path = os.path.join(media_root, file_name)
print("absolute_file_path", absolute_file_path)
file_dir = os.path.dirname(absolute_file_path)
print("file_dir", file_dir)
if not os.path.exists(file_dir):
os.makedirs(file_dir)
return absolute_file_path

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75